简体   繁体   English

React Context API,更新上下文

[英]React Context API, updating context

I would like to set the context at runtime after some networking call is complete (only then do I know the value which needs to be accessible throughout my app), but I don't know how to persist this value. 我想在一些网络调用完成后在运行时设置上下文(只有那时我知道需要在我的应用程序中访问的值),但我不知道如何保持这个值。

I can update the context value like so: 我可以像这样更新上下文值:

<NetworkVersion.Provider value={{version: this.state.version}}>

where I can use the state of the component. 我可以在哪里使用组件的状态。 This approach has been taken from the official React docs. 这种方法取自官方的React文档。

but I was surprised to find out that other Consumers of this Provider get the default value (an empty object) which was initialised in the React.createContext() call. 但我很惊讶地发现此Provider的其他使用者获得了在React.createContext()调用中初始化的默认值(空对象)。 Is there a way to update the Context at runtime and keep that value for the lifetime of the app? 有没有办法在运行时更新Context并在应用程序的生命周期内保留该值?

Make sure your Context Consumer is a child of the associated Provider, otherwise it will just get the default value. 确保您的Context Consumer是关联Provider的子级,否则它将获得默认值。 See https://reactjs.org/docs/context.html#consumer 请参阅https://reactjs.org/docs/context.html#consumer

You'd also be better to pass a primitive value - ie 你也可以更好地传递一个原始值 - 即

<NetworkVersion.Provider value={this.state.version}>

or you may get unnecessary re-renders. 或者你可能会得到不必要的重新渲染。 See https://reactjs.org/docs/context.html#caveats 请参阅https://reactjs.org/docs/context.html#caveats

A consumer must be a child of the provider to get the context. 消费者必须是提供者的孩子才能获得上下文。 If it is impossible to make your consumer be a child of the provider, make a common parent be the provider and add a callback to the context to give consumers a possibility to change the context. 如果不可能使您的消费者成为提供者的子女,那么将共同父母作为提供者并向上下文添加回调以使消费者有可能更改上下文。

 const NetworkVersion = React.createContext({ version: null, setVersion: version => {} }); class Parent extends React.Component { state = { networkContext: { version: null, setVersion: version => this.setState({ networkContext: {...this.state.networkContext, version} }) } }; render() { return <NetworkVersion.Provider value={this.state.networkContext}> <VersionGetter/> <VersionSetter/> </NetworkVersion.Provider>; } } function VersionGetter() { return <NetworkVersion.Consumer> {networkContext => ( <div>Version: {networkContext.version}</div> )} </NetworkVersion.Consumer>; } function VersionSetter() { return <NetworkVersion.Consumer> {networkContext => ( <div> <button onClick={() => networkContext.setVersion('1.0')}>Set version 1</button> <button onClick={() => networkContext.setVersion('2.0')}>Set version 2</button> </div> )} </NetworkVersion.Consumer>; } ReactDOM.render(<Parent/>, document.body); 
 <script src="https://cdn.jsdelivr.net/npm/react@16.4.1/umd/react.production.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/react-dom@16.4.1/umd/react-dom.production.min.js"></script> 

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

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