- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
import React, { useContext } from 'react'
import { MyContext, MyProvider } from './Context'
const MasterContainer = () =>{
const ctx = useContext(MyContext)
return (
<MyProvider>
{ctx}
<MyContext.Consumer>
{context=><div>{context.age}</div>}
</MyContext.Consumer>
</MyProvider>
)
}
export default MasterContainer
当我真正想要拉取 ctx.age 时,ctx 现在返回未定义
import React from 'react'
export const MyContext = React.createContext("dude")
export class MyProvider extends React.Component{
state = {
name: 'Hello',
age: 12
}
render(){
return (
<MyContext.Provider value={this.state}>
{this.props.children}
</MyContext.Provider>
)
}
}
基本上我想使用钩子(Hook)访问我的提供程序中我的状态值,我该怎么做?
最佳答案
这是 createContext
方法和更新当前上下文值的一个非常简单的用例。 CodeSandbox-example
这里需要注意,如 React.js createContext-method documentation 中所述。 ,上下文值将与树中上面最接近的匹配 Provider
进行匹配。
React.createContext - Creates an Context object. When React renders a component that subscribes to this Context object it will read the current context value from the closest matching Provider above it in the tree.
另请记住,如文档中所述,仅当未找到匹配的提供程序时才使用默认值参数。
The defaultValue argument is only used when a component does not have a matching Provider above it in the tree. This can be helpful for testing components in isolation without wrapping them. Note: passing undefined as a Provider value does not cause consuming components to use defaultValue.
关于reactjs - 使用 useContext 钩子(Hook)时如何访问提供者值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53089554/
最近进入一个项目,在react中的useContext看到了以前没见过的东西。 项目中的全局状态使用它发送到上下文的钩子(Hook),然后当稍后在全局状态上调用该钩子(Hook)时,全局状态是可访问的
我使用了一个结构 React Hooks .它基于全局 Context包含 reducer 的组合(如在 Redux 中)。 另外,我广泛使用custom hooks分离逻辑。 我有一个包含异步 AP
在 React 18.1.0 和 React-Dom 18.1.0 中使用 @mui/material 时,我遇到了情感和 react 之间的一系列兼容性问题。这很奇怪,因为我实际上安装了最新版本的
我试图弄清楚如何正确使用 React Context。我对尝试从函数组件外部访问 Context 的问题感到困惑。我收到错误: Line 9:18: React Hook "useContext" i
努力学习如何使用 react 钩子(Hook) .我用 创建上下文 我想将状态和功能一起转移到其他组件,但我需要有关如何执行此操作的帮助。我也想问这样做是否有意义。 事实上,我可以使用 来更新状态设置
我有两个组件 Parent 和 Child,我想将上下文从 Parent 导出到 Child,但这会导致循环依赖。 例如,考虑 Parent.js 是 import {Child} from './C
我对 React 非常着迷,但我遇到了一个关于 useContext 的大问题。真正了解它的内部工作原理是相当令人困惑的。就像它神奇地工作一样。 所以我的主模块中有这样的: export const
更新配置文件时,profile.name 会通过导航链接 (Navigation.js) 中的 useContext() 显示。但是,一旦页面刷新或注销期间,profile.name 就会被清除,pr
我试图以最简单的方式设置 useContext,但它一直返回 undefined。 我已经检查了好几次,但仍然无法破译我遗漏的内容。 上下文.js: import React from 'react'
更新配置文件时,profile.name 会通过导航链接 (Navigation.js) 中的 useContext() 显示。但是,一旦页面刷新或注销期间,profile.name 就会被清除,pr
我试图以最简单的方式设置 useContext,但它一直返回 undefined。 我已经检查了好几次,但仍然无法破译我遗漏的内容。 上下文.js: import React from 'react'
我遇到了一个非常令人沮丧的问题,我不知道发生了什么。我有一个简单的上下文,如下所示: import React, { useState, createContext } from "react"; e
我正在尝试掌握 React 中新的 useContext 函数。在无状态功能组件中效果很好。例如: import React from 'react'; import LocaleContext fr
如果我在 Comp1.js const Comp1 = () => { const globalTheme = new createContext() return (
我无法弄清楚为什么我的 useContext 没有在这个函数中被调用: import { useContext } from "react"; import { MyContext } from ".
我在子组件中有一个按钮 AddMore.js它在列表中添加了更多项目,但它不会重新呈现呈现该列表的其他子组件 Pig.js .我认为我的设置会起作用,因为我使用 NextJS 框架以类似的方式设置了上
你好 我正在尝试进行菜单切换,其中我有一个以 false 作为初始值的变量,使用 react createContext 和 useContext Hook ,我将初始状态设置为 true // us
我有问题,我已经设置了 useContext与 useState提供程序中的值,但是当我导入它们并尝试更改值时,它会在本地更改而不是全局更改! 登录.js import { UserContext }
我正在尝试使用 React Context 来管理我的项目的状态,但这次我似乎无法让它适用于我的 CRA 代码库,而我在其他项目上进行了成功的尝试。 useContext 返回未定义,因此我无法访问上
我是 useContext 的新手钩子(Hook)并试图找出我做错了什么......没有顶级导入,这就是我的代码的样子: 所以我的项目的根是这样的: export default function A
我是一名优秀的程序员,十分优秀!