question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] useAtom(atom) does not re-render when the input atom changes.

See original GitHub issue

Issue 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:closed
  • Created 3 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kueychujcommented, Jan 22, 2021

what’s the definition of todofamily? I think that might give it away 😃

So fast. :p

Thanks for quick response. I have just created a minified example on this. https://codesandbox.io/s/keen-haslett-cnjz9

1reaction
merisbahticommented, Jan 22, 2021

what’s the definition of todofamily? I think that might give it away 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found