[Bug] useAtom(atom) does not re-render when the input atom changes.
See original GitHub issueIssue updated in https://github.com/pmndrs/jotai/issues/273#issuecomment-765399339
Item Component won’t re-render when id
changes.
import * as React from "react";
import { atomFamily } from "jotai/utils";
import { useAtom } from "jotai";
const todoFamily = atomFamily<number, any>((id) => ({
id
}));
const Item: React.FC<{ id: number }> = ({ id }) => {
const todo = useAtom(todoFamily(id));
return <h1>{JSON.stringify(todo)}</h1>;
};
export default function App() {
const [a, setA] = React.useState(1);
return (
<div className="App">
<button onClick={() => setA((a) => a + 1)}>+</button>
<button onClick={() => setA((a) => a - 1)}>-</button>
<Item id={a} />
</div>
);
}
Sandbox Example https://codesandbox.io/s/keen-haslett-cnjz9
any thought?
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (5 by maintainers)
Top Results From Across the Web
The component is re-rendered the first time it is rendered. #1137
useState and recoil.js will not re-render the component on first initialization, but will re-trigger the rendering when the state changes.
Read more >Jotai + React: unexpected re-rendering, and ... - Stack Overflow
When the energy reaches 0 , a useEffect in the <EnergyLevel /> component triggers a change in a "global event state" atom. That...
Read more >Jotai: The Ultimate React State Management - 100ms
The thing is when you update this atom, all the components relying on this atom will rerender. Even if you change just the...
Read more >Atom Family in Recoil for Statement Management — Nextjs
This blog post will teach you how to use the recoil atom family to update the specific atom in recoil state management.
Read more >Jotai: Atom-based state management for React
Jotai's has a simple API which consists of API methods like Provider , atom , and useAtom . Unlike Recoil, it doesn't require...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
So fast. :p
Thanks for quick response. I have just created a minified example on this. https://codesandbox.io/s/keen-haslett-cnjz9
what’s the definition of todofamily? I think that might give it away 😃