jotai: freezeAtom usage is unclear

Description

I use freezeAtom & freezeAtomCreator with useAtom , and set the new value to test if it can change. And it did change! Does I understand it wrong? Or does freezeAtom not work well?

Reproduce the bug

import { atom, useAtom, Provider } from 'jotai'
import { freezeAtom } from 'jotai/utils'

const countAtom = atom({count:0});

const Increment = () => {
  const freezeCount = freezeAtom(countAtom);
  const [freezeObj, setFreezeObj] = useAtom(freezeCount);
  const inc = () => setFreezeObj({
    ...freezeObj,
    count: freezeObj.count+1
  });
  return (
    <div>
      <button onClick={inc}>+1</button>
      count: {freezeObj.count}
    </div>
  )
}

export default function App() {
  return (
    <Provider>
      <div className="App">
        <Increment />
      </div>
    </Provider>
  );
}

Log

Should be 0 image

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (15 by maintainers)

Most upvoted comments

  {
    ...freezeObj,
    count: freezeObj.count+1
  }

☝️ That is a valid usage. What freezeAtom prevents is this usage. 👇

  freezeObj.count++