pmndrs / swc-jotai

MIT License
90 stars 14 forks source link

React refresh overwrites array of anonymous atoms on assignment #26

Closed nickbabcock closed 3 days ago

nickbabcock commented 6 months ago
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 !
hikariNTU commented 2 months ago

I've added this to my issue reproduction as well, seems like they are similar issue where atom is included in array and some assignment is not linked correctly

image

https://github.com/hikariNTU/jotai-swc-test