【问题标题】:Apollo useReactiveVar throws TypeError: rv.onNextChange is not a functionApollo useReactiveVar 抛出 TypeError: rv.onNextChange is not a function
【发布时间】:2021-05-30 08:40:01
【问题描述】:

我在cache.js 中有以下变量:

export var isLoggedInVar = () => {
  return localStorage.getItem("token") ? true : false;
};

然后,我在 React 函数组件中导入变量并将其分配给反应变量:

const value = useReactiveVar(isLoggedInVar);

但是,在渲染时,如果使用value,我会得到

Uncaught TypeError: rv.onNextChange is not a function

请注意,其他不是函数的变量可以正常工作。如果变量本身没有问题,我该怎么做?

【问题讨论】:

  • 嘿@Ruham,你不能在需要检查值的地方使用isLoggedInVar()吗?在这种情况下,我看不出您需要使用reactiveVariables 的原因..
  • 可以,但渲染不正确。

标签: reactjs apollo-client


【解决方案1】:

在声明时考虑将isLoggedInVar 包装在makeVar 钩子中。

const isLoggedInVar = makeVar(true)

【讨论】:

    猜你喜欢
    • 2018-05-02
    • 2015-11-06
    • 2014-01-31
    • 2021-01-07
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多