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

Nested RecoilRoot using initializeState has unexpected results #2299

Open
mildfuzz opened this issue Jan 15, 2024 · 0 comments
Open

Nested RecoilRoot using initializeState has unexpected results #2299

mildfuzz opened this issue Jan 15, 2024 · 0 comments

Comments

@mildfuzz
Copy link

mildfuzz commented Jan 15, 2024

Our app uses Recoil, but also consumes a library component that also use Recoil. They both use initializeState. The expectation is that we can initialise atoms related to the outer store in the outer RecoilRoot, and atoms related to the inner store in the inner RecoilRoot. I have contrived a reduced example of what we're trying to achieve:

function Comp() {
  const outer = useRecoilValue(outerState)
  const inner = useRecoilValue(innerState)
  return (
    <div>
      <pre>{JSON.stringify({outer, inner}, null, 4)}</pre>
    </div>
  )
}
function App() {
  return (
    <RecoilRoot
      key={'outer'}
      initializeState={({set}) => {
        console.log('outer')

        set(outerState, 'outer state')
      }}>
      <RecoilRoot
        key={'inner'}
        initializeState={({set}) => {
          console.log('inner')
          set(innerState, 'inner state')
        }}>
        <Comp />
      </RecoilRoot>
    </RecoilRoot>
  )
}

The expected output of the above is:

{
    "outer": "outer state,
    "inner": "inner state"
}

The actual result is:

{
    "outer": null,
    "inner": "inner state"
}
@mildfuzz mildfuzz changed the title Nestes RecoilRoot with initialize state has unexpected results Nested RecoilRoot using initializeState has unexpected results Jan 15, 2024
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