【问题标题】:React Hook Component using old value of state which is passed through useContext使用通过 useContext 传递的旧状态值反应 Hook 组件
【发布时间】:2020-06-17 06:34:45
【问题描述】:

请看这个沙盒:

https://codesandbox.io/s/use-context-simple-qygdz?file=/src/App.js

*** 你必须去 /check1 开始,当你到达 /check2 时不应该有 ddd,但它现在仍然存在(状态未更新)

当我将一个页面链接到另一个页面时,usecontext 不会传递状态。不知道为什么 - 但我很高兴在帮助下我们能够准确找出问题所在。

【问题讨论】:

    标签: javascript node.js reactjs


    【解决方案1】:

    如果你只使用一个 useState 钩子来更新你的整个上下文,也许它会有所帮助。我包括了下面的主要部分(这里是一个工作 sample 的链接)。当我尝试这个时,我看到每个组件中的上下文都发生了变化。

    import React from "react";
    import "./styles.css";
    import ChangeContext from "./components/ChangeContext";
    import ViewChange from "./components/ViewChange";
    
    const info = {
      artists: null,
      messages: null,
      songs: null,
      userid: "ddd",
      accesstoken: null,
      refreshtoken: null
    };
    
    export const InfoContext = React.createContext();
    
    export default function App() {
      const [context, setContext] = React.useState(info);
      return (
        <InfoContext.Provider value={[context, setContext]}>
          <div className="App">
            <ChangeContext />
            <ViewChange />
          </div>
        </InfoContext.Provider>
      );
    }
    

    然后在一个组件中

    import React from "react";
    import { InfoContext } from "../App";
    
    export default function App() {
      const [context, setContext] = React.useContext(InfoContext);
      return (
        <div className="App">
          <h1>{context.userid} uid</h1>
          <button
            onClick={e => {
              setContext({ ...context, userid: 123 });
            }}
          >
            click me
          </button>
        </div>
      );
    }
    

    在另一个组件中检查更改

    import React from "react";
    import { InfoContext } from "../App";
    
    export default function ChangeContext() {
      const [context, setContext] = React.useContext(InfoContext);
      return (
        <div className="App">
          <h1>{context.userid} uid</h1>
          <button
            onClick={e => {
              setContext({ ...context, userid: 123 });
            }}
          >
            click me
          </button>
        </div>
      );
    }
    

    不妨试试这个

    const [context, setContext] = useState(info);
      return (
        <BrowserRouter>
          <Route exact path="/signup/:id/:access_token" render={() => <InfoContext.Provider value={[context, setContext]}><Signup /> </InfoContext.Provider>} />
          <Route exact path="/" render={() => <Login />} />
          <Route exact path="/home/:id/:access_token/:refresh_token" render={() => <Homepage ></Homepage>} />
          <Route exact path="/artist/:artistid" render={() => <ArtistPage ></ArtistPage>} />
          <Route exact path="/map" render={() => <MapLeaflet />} />
        </BrowserRouter>
      );
    

    【讨论】:

    • 所以我使用您上面指出的内容重新完成了所有这些操作,但仍然无法正常工作。你能看看我的git吗? github.com/2022karanbhasin/finalnearify 有问题的两个文件在客户端文件夹中,分别是artisthomepage.js 和homepage.js !!无论如何感谢您的帮助!
    • 我添加了另一个选项,可能是将渲染的组件一个一个地包装在提供程序中。我尝试在本地设置您的应用程序以进行测试,但由于需要令牌而无法正常工作。
    • 你能给我你的电子邮件吗?我不介意将它作为 ZIP 发送给您。
    • 所以我更多地查看了您的代码模板,当我尝试这样做时仍然有一个浏览器路由器并链接 changecontext 组件,以便当您单击按钮时它会更改上下文并将您重定向到查看更改上下文,然后它不起作用-->您无法在视图更改上下文组件中看到更改的上下文。 @Henri De Bel
    • 我能够更新 sandbox.io 以显示错误/问题!这是:codesandbox.io/s/use-context-simple-qygdz?file=/src/App.js
    【解决方案2】:

    我还不能发表评论,但 userId 是否正在上下文中更新?
    artisthomepage.jsconsole.log(userid) 的值是多少?也许它使用旧值呈现,但随后它接收新值并且不重新呈现组件。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 2021-06-24
    • 2020-01-31
    • 2019-10-10
    • 2021-03-16
    • 2019-07-14
    • 2019-01-07
    相关资源
    最近更新 更多