为什么分配给默认值的上下文值?
为什么在提供商中使用了值支柱,为什么分配给默认值的上下文值? 这是当我创建上下文时:
export let exchangeContext=React.createContext();
我在同一组件中创建了提供商
<exchangeContext.Provider value={"aya"}> </exchangeContext.Provider>
,当我安装消费者中的值时,我会被不确定为默认值
<exchangeContext.Consumer>
{(value)=>{
console.log(value)
why do the context value assigned to default value despite of using value prop in provider??
here is when I created the context:
export let exchangeContext=React.createContext();
I created provider in the same component
<exchangeContext.Provider value={"aya"}> </exchangeContext.Provider>
and when I console log the value in the consumer i get undefined as the default value
<exchangeContext.Consumer>
{(value)=>{
console.log(value)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不必写消费者
消费者是usecontext挂钩是一件事情之前的旧方法),则
如果您使用上下文(
。
写作 ://reactjs.org/docs/hooks-reference.html#usecontext“ rel =” nofollow noreferrer“> https://reactjs.s.org/docs/hooks/hooks-reference.html#usecontext
You don't have to write consumer if you used context (writing consumer is the old way before useContext hook was a thing)
In your consumer file do this
then in your function body
then in your return body
see: https://reactjs.org/docs/hooks-reference.html#usecontext for more info