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.

Atoms value becomes `undefined` after using React.useCallback in useSelector (with atomFamily)

See original GitHub issue

Hey,

This is quite elaborate, but I havent managed to make a smaller example.

Buggy code is here: https://github.com/merisbahti/jotai-optics/blob/meris/clean-arrayfamily/example/index.tsx

To run it, just clone the repo and cd example; yarn start.

The problem appears when removing a field in the form (using the X button), then adding a new field… This resutls in an exception where the value of the atom is undefined!

issue seems to be with useAtomArrayFamily, lines here: https://github.com/merisbahti/jotai-optics/blob/meris/clean-arrayfamily/src/index.ts#L92-L103

With the useCallback the bug appears:

   // other from useAtomArrayFamily truncated
  return useSelector(
    atom,
    React.useCallback(
      elements => {
        const length = elements.length
        return Array.from(new Array(length)).map(
          (_, key) => [atomFamilyGetter(key), () => removeItem(key)] as const,
        )
      },
      [atomFamilyGetter, removeItem],
    ),
  )

but without the React.useCallback, the bug doesnt appear.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
merisbahticommented, Nov 6, 2020

Actually, dai-shi, it seems like the error is in my side,

I actually took the time to implement the bug in my library on this branch: https://github.com/merisbahti/klyva/tree/meris/klyva-next

So I’ll spend some time to figure out what I’m doing wrong, thanks for your help.

I’ll try and keep this issue posted as I find out more, but you can close it if you want. (I’ll still write here even if its closed)

1reaction
merisbahticommented, Nov 5, 2020

currently at work but will check when finished.

Read more comments on GitHub >

github_iconTop Results From Across the Web

getting undefined from useSelector react-redux hook
I have a component that dispatches action to the reducer using useDispatch hook inside useEffect. I only wants it to happen once on...
Read more >
What happened to Components being just a visual thing?
I really like when Components are actually just visual components. Like the JSX specification... Tagged with react, solidjs, javascript, ...
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