Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UseContext cannot access context correctly #382

Open
xieyongyi0614 opened this issue Sep 10, 2024 · 0 comments
Open

UseContext cannot access context correctly #382

xieyongyi0614 opened this issue Sep 10, 2024 · 0 comments

Comments

@xieyongyi0614
Copy link

Is useContext currently in the testing phase? After changing the isShow parameter of the context to true and rendering the TestConsumer component, the context obtained within the component is the default value, not the updated value after isShow has changed. Removing the isShow condition from {isShow && } under TestingContext.Provider, TestConsumer and its TestShow component also include a value.show. But at this time, TestShow is able to correctly obtain the value of value, not the default value.
`
export const useTest = () => {
const [value, setValue] = useState(Math.random().toString());
const [isShow, setIsShow] = useState(false);
return {
value,
isShow,
setIsShow,
};
};
const testContextValue = useTest();
const { value, isShow, setIsShow } = testContextValue;
useEffect(() => {
setTimeout(() => {
setIsShow(true);
}, 2000);
}, []);
<TestingContext.Provider value={testContextValue}>



{isShow && }


</TestingContext.Provider>
const TestConsumer = () => {
const value = useContext(TestingContext) as any;

return (
<>
{JSON.stringify(value)}
{value.isShow && }
111
</>

);
};
export default memo(TestConsumer);
const TestShow = () => {
const { value, isShow } = useContext(TestingContext) as any;
return

Fast list item{value}
;
};
export default memo(TestShow);
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant