[英]How to properly change React context value?
Say i've declared have the following context:假设我已声明具有以下上下文:
const ColorContext = React.createContext(“”)
I am trying to create a simple function that can change the value of the context, which can later be used in a number of other components as a global state variable.我正在尝试创建一个简单的 function ,它可以更改上下文的值,稍后可以在许多其他组件中用作全局变量 state 。 The function i've implemented however is not doing anything to change the context value, here is what i've done, feel free to let me know where I am going wrong in the code:
我已经实现的 function 但是没有做任何改变上下文值的事情,这是我所做的,请随时告诉我我在代码中哪里出错了:
function GlobalContext(){
const {color, setColor} = useContext(ColorContext);
const toGreen = () => {
setColor(‘green’);
};
return(
<>
<ColorContext.Provider value={{color, setColor}}>
<button onClick={toGreen}> Change Color </button>
<ColorContext.Provider/>
</>
)
}
You're doing it wrong.你这样做是错的。
useContext
is for use in components lower in the tree than the Context's Provider. useContext
用于树中低于上下文提供者的组件。 What you want in the component that renders your Context.Provider
is this:您在呈现
Context.Provider
的组件中想要的是:
const [color, setColor] = useState();
So the whole thing would look something like this:所以整个事情看起来像这样:
const ColorContext = React.createContext();
const MyColorContextProvider = ({children}) => {
const [color, setColor] = useState();
const toGreen = () => setColor('green');
return (
<ColorContext.Provider value={{color,setColor}}>
{children}
<button onClick={toGreen}>Change to Green</button>
</ColorContext.Provider>
);
}
and then:接着:
const MyChildComponent = () => {
const {color} = useContext(ColorContext);
return <>The color in context is {color}</>
}
your app could be:您的应用可能是:
const App = () => {
return (
<MyColorContextProvider>
<MyChildComponent/>
</MyColorContextProvider>
);
}
Stackblitz: https://stackblitz.com/edit/react-gym2ku?file=src%2FColorContextProvider.js Stackblitz: https://stackblitz.com/edit/react-gym2ku?file=src%2FColorContextProvider.js
toGreen()
is passed as an argument to the value of ColorContext
toGreen()
作为参数传递给ColorContext
的值toGreen()
to change the colortoGreen()
来改变颜色// Created a New Context for global use
const ColorContext = createContext();
export default function GlobalContext() {
// I set the value of color to red using array destructuring
const [color, setColor] = useState('Red');
const toGreen = () => {
// I set the color to green when this function is called
setColor('Green');
}
return (
<div>
{/* I pass the both the color and the toGreen function as args, now they can be accessed by all nested components */}
<ColorContext.Provider value= {{color, toGreen}}>
<h1>Current Color is : {color}</h1>
<NestedComponent />
</ColorContext.Provider>
</div>
);
}
// I created a nested component
function NestedComponent() {
// useContext lets us access the values passed to ColorContext
const colorProvider = useContext(ColorContext);
return (
// When button is Clicked the toGreen function passed down is triggered
<button onClick={colorProvider.toGreen}>Change Color from {colorProvider.color} to Green</button>
);
}
Explanation and Code in Code Sandbox Hope it helps代码沙箱中的说明和代码希望对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.