jotaijs / jotai-signal

MIT License
233 stars 3 forks source link

Invalid value used as weak map key #13

Closed janlin002 closed 1 year ago

janlin002 commented 1 year ago

I'm trying to follow a tutorial, but I encountered the following problem: "Invalid value used as weak map key". How can I solve this issue?

janlin002 commented 1 year ago
截圖 2023-03-22 上午11 55 53
dai-shi commented 1 year ago

https://codesandbox.io/s/github/jotai-labs/jotai-signal/tree/main/examples/01_typescript?file=/src/App.tsx Just opened it and it seems to be working. Can you modify it to reproduce the error?

janlin002 commented 1 year ago

I have solved the problem. I realized that I imported useAtom and useSetAtom incorrectly, I wrote jotai instead of jotai/react.

dai-shi commented 1 year ago

Hmm? Wouldn't it be the same?

import { useAtom } from 'jotai';
import { useAtom } from 'jotai/react';
janlin002 commented 1 year ago

https://codesandbox.io/s/recursing-cray-j0c9uc?file=/src/App.js This is a simple demo and I noticed that if I use "$" like a useState hook(const [a, b] = useAtom(initvalue)), it will result in an error(Invalid value used as weak map key). However, if I declare them separately, it works correctly.

dai-shi commented 1 year ago

You want to pass an atom object to $(), so in your case, it's $(initCount).

janlin002 commented 1 year ago

Understood, thank you for your response.