admin管理员组文章数量:1293179
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 ponents as a global state variable. 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 GlobalContext(){
const {color, setColor} = useContext(ColorContext);
const toGreen = () => {
setColor(‘green’);
};
return(
<>
<ColorContext.Provider value={{color, setColor}}>
<button onClick={toGreen}> Change Color </button>
<ColorContext.Provider/>
</>
)
}
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 ponents as a global state variable. 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 GlobalContext(){
const {color, setColor} = useContext(ColorContext);
const toGreen = () => {
setColor(‘green’);
};
return(
<>
<ColorContext.Provider value={{color, setColor}}>
<button onClick={toGreen}> Change Color </button>
<ColorContext.Provider/>
</>
)
}
Share
Improve this question
asked Sep 19, 2021 at 22:30
subprimeloadssubprimeloads
3924 silver badges15 bronze badges
2 Answers
Reset to default 6You're doing it wrong. useContext
is for use in ponents lower in the tree than the Context's Provider. What you want in the ponent that renders your Context.Provider
is this:
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./edit/react-gym2ku?file=src%2FColorContextProvider.js
toGreen()
is passed as an argument to the value ofColorContext
- Any Nested Component can use
toGreen()
to change the color
// 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 ponents */}
<ColorContext.Provider value= {{color, toGreen}}>
<h1>Current Color is : {color}</h1>
<NestedComponent />
</ColorContext.Provider>
</div>
);
}
// I created a nested ponent
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
本文标签: javascriptHow to properly change React context valueStack Overflow
版权声明:本文标题:javascript - How to properly change React context value? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741567989a2385837.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论