简体   繁体   English

当导入ReactJS时,为什么useContext不更新我的值?

[英]Why isn't useContext updating my value when imported in ReactJS?

I'm trying to use the createContext and useContext features of ReactJS to create a notification icon that displays a number of notifications increasing by 1 each second (essentially trying to figure out how to pass a state between files/components), but am unable to get the value to transfer over. 我正在尝试使用ReactJS的createContext和useContext功能来创建一个通知图标,该图标显示每秒以1递增的通知数量(本质上是试图弄清楚如何在文件/组件之间传递状态),但是无法获得转移的价值。

The timer appears to be working fine and the seconds variable in the timer file is definitely increasing, it's just the secs in the NavBar that isn't updating correctly. 计时器似乎工作正常,并且计时器文件中的seconds变量肯定在增加,只是NavBar中的秒数未正确更新。

--- App File --- -应用文件-

function App() {
  return (
    <div>
      <NavBar />
      <Timer />
    </div>
  );
}
export default App;

---Timer File--- -计时器文件-

...
export const secContext = createContext()
const Timer = () => {
    const [seconds, setSeconds] = useState(0);
    ...
    useEffect(() => {
        ...
        if (isActive) {
            interval = setInterval(() => {
            setSeconds(seconds => seconds + 1);
        }, 1000);}
        ...

return (
    ...
        <secContext.Provider value={seconds}>
        <div className="time">
            {seconds}s
        </div>
        ...
        </secContext.Provider>
    ...
    );
};

---Navigation Bar File--- ---导航栏文件---

...
import {secContext} from './Timer'
export default function NavBar() {
  const secs = useContext(secContext)

  return (
          ...
          <Badge badgeContent={secs}>
            <NotificationsIcon />
          </Badge>
          ...
   );
}

I was expecting {secs} in the Navigation Bar file to update to be equal the value of the seconds value from the Timer, but instead it appears to remain null. 我期望导航栏文件中的{secs}更新为等于Timer的秒值,但是它似乎保持为空。 Here is a screenshot of the interface: https://gyazo.com/d283360091c9d4ea8d9b2785419ad665 In this case the notification icon should have a badge with the number 10. 以下是界面的屏幕截图: https : //gyazo.com/d283360091c9d4ea8d9b2785419ad665在这种情况下,通知图标应带有带有数字10的徽章。

Context values are only accessible if they are rendered within the hierarchy ie 如果上下文值在层次结构中呈现,则只能访问它们

<secContext.Provider value={x}>
  <NavBar />
</secContext.Provider>

NavBar doesn't appear to be a descendant of your context, your context is created inside Timer and therefore is only accessible from descendants of that component eg NavBar似乎不是您上下文的后代,您的上下文是在Timer内部创建的,因此只能从该组件的后代访问,例如

<Timer>
  <NavBar />
</Timer>

Update 更新资料

As per the comments discussion, if you simply want to share data between a couple of siblings, it would probably make more sense to just move the common up to the parent ( App ) and then pass the data into the siblings as props - see example . 按照意见的讨论,如果你只是想几个兄弟姐妹之间共享数据,它可能会更有意义,只要将共同到父( App ),然后将数据传递到兄弟姐妹的道具-见例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM