score:1

Accepted answer

there are 2 problems here.

  1. the appprovider should be a parent of app. this way it can fully access the context.
const rootelement = document.getelementbyid("root");
render(
  <appprovider>
    <app />
  </appprovider>,
  rootelement
);
  1. dispatch is changing the state async so console.log just after dispatch logs the old value. you should "watch" the change using useeffect.
react.useeffect(() => {
  console.log("effect", state.show);
}, [state.show]);

a working demo: https://codesandbox.io/s/peaceful-blackwell-ohd0y?file=/src/index.tsx


Related Query

More Query from same tag