Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Children components don't see update of one atom in parent one #2549

Closed
2 tasks
AlekseyP18 opened this issue May 10, 2024 · 1 comment
Closed
2 tasks

Children components don't see update of one atom in parent one #2549

AlekseyP18 opened this issue May 10, 2024 · 1 comment

Comments

@AlekseyP18
Copy link

Summary

Hello dear jotai team. I liked this store manager because it has all needed features. I've been trying to migrate from mobx store to jotai, and I'm facing with one bug.

I have 2 components App and ParentFruit which use one atom - pricesAtom

When I added new price in App component the child component - ParentFruit doesn't see this new one object and doesn't render this new one

There is the same problem with arrays too - todosAtom

Link to reproduction

Link to sandbox reprodaction

Check List

Please do not ask questions in issues.

  • I've already opened a discussion before opening this issue, or already discussed in other media.

Please include a minimal reproduction.

@rothsandro
Copy link
Collaborator

rothsandro commented May 10, 2024

The problem is where your Provider is placed. The useAtom call in your App component is not wrapped in a provider and therefore uses the default store of Jotai. Your ParentFruit component is wrapped in a Provider and therefore uses a separate store, not the default store.

const App = () => {
  const [...] = useAtom(fruitsAtom);    // (1) Default store

  return (
    <Provider>
      <ParentFruit />                   // (2) Store from Provider
      <button>Add Fruit</button>
    </Provider>
  );
};

The Provider should be at the root of your application, everything should be wrapped in the provider.

const App = () => {
  return (
    <Provider>
      <DeliciousFruits />
    </Provider>
  );
}

const DeliciousFruits = () => {
  const [...] = useAtom(fruitsAtom);

  return (
    <ParentFruit />
    <button>Add Fruit</button>
  );
};

PS. I think it's better to open a discussion instead of an issue for the next time.

@pmndrs pmndrs locked and limited conversation to collaborators May 10, 2024
@dai-shi dai-shi converted this issue into discussion #2550 May 10, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants