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.

jotai/urql client getter get a wrong atom value

See original GitHub issue

Hi, I am trying to use jotai to manage the state of an app with a login system. When a user login, app will receive a token and the app will use it to create an urql client with the token as the bearer key.

However, the app will crash whenever the login state change like this: login -> logout -> login (crash!), and saying Cannot read property 'query' of null . It looks like the getter in atomWithQuery somehow fetched an old value, but when I try to log the client they get, it all look fine (showing a client object). I wonder if I missed something, or it is a bug.

A small demo to the problem, the app will crash when you attempt to login the second time: Link
Stack-trace

TypeError
Cannot read property 'query' of null
Object.eval [as read]
https://codesandbox.io/static/js/sandbox.68fd4509e.js:1:100695
readAtomState
https://3mi3y.csb.app/node_modules/jotai/esm/index.js:258:33
eval
https://3mi3y.csb.app/node_modules/jotai/esm/index.js:261:25
Object.eval [as read]
https://codesandbox.io/static/js/sandbox.68fd4509e.js:1:100695
readAtomState
https://3mi3y.csb.app/node_modules/jotai/esm/index.js:258:33
eval
https://3mi3y.csb.app/node_modules/jotai/esm/index.js:241:13
readAtomState
https://3mi3y.csb.app/node_modules/jotai/esm/index.js:237:19
readAtom
https://3mi3y.csb.app/node_modules/jotai/esm/index.js:319:21
eval
https://3mi3y.csb.app/node_modules/jotai/esm/index.js:643:23
checkForUpdates
https://3mi3y.csb.app/node_modules/jotai/esm/index.js:69:30
eval
https://3mi3y.csb.app/node_modules/jotai/esm/index.js:521:63
eval
https://3mi3y.csb.app/node_modules/jotai/esm/index.js:521:43
flushPending
https://3mi3y.csb.app/node_modules/jotai/esm/index.js:511:11
eval
https://3mi3y.csb.app/node_modules/jotai/esm/index.js:406:5
Object.config.write
https://3mi3y.csb.app/node_modules/jotai/esm/index.js:632:7
writeAtomState
https://3mi3y.csb.app/node_modules/jotai/esm/index.js:384:30
writeAtom
https://3mi3y.csb.app/node_modules/jotai/esm/index.js:417:3
updateAtom
https://3mi3y.csb.app/node_modules/jotai/esm/index.js:562:40
eval
https://codesandbox.io/static/js/sandbox.68fd4509e.js:1:100695
eval
/src/App.js:59:4
  56 | const [token, setToken] = useState(null);
  57 | 
  58 | useEffect(() => {
> 59 |   setClient(generateClient(token));
     |  ^
  60 | }, [token]);
  61 | 
  62 | return (
View compiled
▶ 12 stack frames were collapsed.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:28 (25 by maintainers)

github_iconTop GitHub Comments

2reactions
Thisencommented, Aug 3, 2021

Makes sense. Thanks for your help. Let’s see if someone has some interests in this. cc @Thisen

I’ll look into it.

2reactions
israelidannycommented, Aug 2, 2021

I added a few tests for the usecase that is supported by types right now. The tests are passing. I thought it’s an important step (to not break the already working functionality if we want to catch the timing issue), it’s also good to have tests for this as this is a common use case (re-initializing client based on authentication for example, like in my use case): https://github.com/pmndrs/jotai/pull/634

Read more comments on GitHub >

github_iconTop Results From Across the Web

URQL — Jotai, primitive and flexible state management for ...
The last optional getClient parameter is a function that returns Client. The return values have two atoms. The first one is called dataAtom...
Read more >
jotai atomwithhash - You.com | The Search Engine You Control
It looks like the getter in atomWithQuery somehow fetched an old value, but when I try to log the client they get, it...
Read more >
Jotai: The Ultimate React State Management - 100ms
This atom can store value in sessionStorage too, so the atom's value will be persisted until the browser is closed.
Read more >
jotai: Versions - Openbase
Now, useAtomDevtools in jotai/devtools supports read-only atoms. You can only see the read-only atom values in Redux DevTools Extention (you can't change the ......
Read more >
Index of /repositories/external-npm
babel-helper-get-function-arity/, Wed Nov 04 22:12:42 EST 2015. babel-helper-hoist-variables/ ... browser-sync-client/, Tue Nov 01 22:43:38 EDT 2022.
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