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

data in store changed without dispatching any actions #303

Closed
hronro opened this issue Mar 2, 2020 · 5 comments
Closed

data in store changed without dispatching any actions #303

hronro opened this issue Mar 2, 2020 · 5 comments

Comments

@hronro
Copy link

hronro commented Mar 2, 2020

CodeSandbox

Steps to reproduce in this CodeSandbox demo:

  1. In home page, add some todo lists
  2. Click the "Go to 404 page" link to go to 404 page (this step doesn't trigger any reatom actions)
  3. In 404 page, click "Back to home page" link to go to (this step doesn't trigger any reatom actions)
  4. The data in home page has been reset to the initial state (which is unexpected)

It is so weird, I use the devtools to check the reatom store after step1, I found the store is correct at that time. But after some router changes (without dispatching any actions), the store has been changed...

@stenin-nikita
Copy link
Contributor

After unmounting the component, the atoms are unsubscribed and the value in the store is cleared. There are two solutions to save the state between page transitions:

  1. to subscribe on atom in parent component
const App = () => {
  useAtom(todoListsAtom, () => null, [])

  return (
    <Router>
      <Home path="/" />
      <NotFound default />
    </Router>
  )
}

render(<Provider value={store}><App /></Provider>, rootElement);
  1. to create store with initial atom
const store = createStore(todoListsAtom)

@stenin-nikita
Copy link
Contributor

Hmm. When using symbols as the name of an atom, the state is cleared too. I don't think it should be like this.

@hronro
Copy link
Author

hronro commented Mar 3, 2020

@stenin-nikita I tried solution 2 before, and I tried use Symbols or Strings as atom name, neither of them works. The solution 1 works for me as a workaround, but I hope we can have a better solution.

@artalar
Copy link
Owner

artalar commented Mar 3, 2020

@foisonocean @stenin-nikita thx for it discussion! We must to do some improvements and I create issues for it.

@artalar
Copy link
Owner

artalar commented Jun 17, 2021

#305 and #306 is solved. Remaining discussion should follow #304

@artalar artalar closed this as completed Jun 17, 2021
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

3 participants