Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React refresh overwrites array of anonymous atoms on assignment #26

Open
nickbabcock opened this issue Mar 4, 2024 · 0 comments
Open

Comments

@nickbabcock
Copy link

nickbabcock commented Mar 4, 2024

import {atom} from "jotai";
const abc = [atom("message")];
console.log(abc.length):
// Outputs undefined

Instead abc is now an atom (init, read, write properties), so I believe something is causing abc to be overwritten.

Amusingly, the following works as expected

import {atom} from "jotai";
console.log([atom("message")].length):
// Outputs 1

If I remove @swc-jotai/react-refresh from the list of swc plugins, it works. What could be the problem?

It seems to be a general problem where assigning a variable with an expression that involves atoms will assume that the variable is the atom.

const abc = () => [atom('message')]
// abc is once again an atom

A workaround is to wrap the construction in a named function.

function abc() {
  return [atom('message')];
}
const foo = abc();
console.log(foo.length) // 1 !
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant