Next.js Discord

Discord Forum

Provider pattern. Unable to retrieve a provider's state from within another provider

Unanswered
Virginia's Warbler posted this in #help-forum
Open in Discord
Virginia's WarblerOP
My app have many providers
<FirstProvider>
  <SecondProvider>
    <App />
  </SecondProvider>
</FirstProvider>

The first provider stores a state variable (ie: const [flag, setFlag] = useState(false))
In the app files, I can correctly set and read the flag variable.
But in the <SecondProvider>, I am unable to read the most updated value of flag, it seems that from within <SecondProvider> I am getting the initial value of flag.

Could anyone please help figure out what could cause this weird behavior?

1 Reply

Virginia's WarblerOP
Another clue:

When I use State or MMKV, I am unable to read the most updated value of flag from within <SecondProvider>, even when significant time has passed between updating and reading of the flag variable.

But when I useRef, I can read the most updated value of flag from everywhere