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

Bug: passing an object to mutableAtom is not registered to a provider. #9

Open
alarbada opened this issue Jul 3, 2024 · 9 comments · May be fixed by #10
Open

Bug: passing an object to mutableAtom is not registered to a provider. #9

alarbada opened this issue Jul 3, 2024 · 9 comments · May be fixed by #10

Comments

@alarbada
Copy link

alarbada commented Jul 3, 2024

Let me explain in code:

// this one updates as expected
const counterAtom = atom(0)
// this one aswell. The value is not shared when using Provider
const counterMutableAtom = mutableAtom(0)
// this one seems to ignore the Provider
const counterMutableObjectAtom = mutableAtom({ count: 0 })

function Counter() {
  const [count, setCount] = useAtom(counterAtom)
  const [countMutable] = useAtom(counterMutableAtom)
  const [countMutableObject] = useAtom(counterMutableObjectAtom)

  return (
    <>
      <div>
        <p>Count: {count}</p>
        <Button onClick={() => setCount((c) => c + 1)}>Increment</Button>
      </div>
      <div>
        <p>Count mutable: {countMutable.value}</p>
        <Button onClick={() => countMutable.value++}>Increment</Button>
      </div>
      <div>
        <p>Count mutable: {countMutableObject.value.count}</p>
        <Button onClick={() => countMutableObject.value.count++}>
          Increment
        </Button>
      </div>
    </>
  )
}

function ExampleOfBug() {
  return (
    <>
      <Counter></Counter>
      <Counter></Counter>
      <Provider>
        <Counter></Counter>
      </Provider>
    </>
  )
}

I've got no idea why, but using an object inside a mutableAtom makes it skip the Provider.

@dai-shi
Copy link
Member

dai-shi commented Jul 3, 2024

Nice catch!

I assume you are using Valtio v2.

The initialValue is shared across stores. We should deepClone when passing to atom().

const valueAtom = atom({ value: initialValue });

@alarbada
Copy link
Author

alarbada commented Jul 3, 2024

Oh I'm so sorry, I did not include the versions of the packages.

The example is using "jotai": "^2.8.4" and "jotai-valtio": "^0.5.0". It's not using valtio directly.

I just installed valtio 1.13.2 as a peer dependency, and I can still reproduce the bug.

@dai-shi
Copy link
Member

dai-shi commented Jul 3, 2024

Hm, okay, yeah, it may happen with Valtio v1 because it's the same reference.

Do you think you can open a PR? Otherwise, I can do that.

import { deepClone } from 'valtio/utils'; // only available in Valtio v2

  const valueAtom = atom({ value: deepClone(initialValue) }); 

@alarbada
Copy link
Author

alarbada commented Jul 3, 2024

pls open the pr, I tried but I couldn't find out how to import valtio v2 :S

@dai-shi
Copy link
Member

dai-shi commented Jul 4, 2024

No problem. Yeah, it's not very straightforward to upgrade to Valtio v2. Working on it.

@dai-shi dai-shi linked a pull request Jul 4, 2024 that will close this issue
3 tasks
@dai-shi
Copy link
Member

dai-shi commented Jul 4, 2024

Can you try #10?
https://ci.codesandbox.io/status/jotaijs/jotai-valtio/pr/10
☝️ Find "Local Install Instructions"


#9 (comment) was wrong. It's a different line to apply deepClone.

@alarbada
Copy link
Author

alarbada commented Jul 4, 2024

Nice, it works! Thanks man

I had to bun install [email protected] (I did not know about npm view valtio versions).

How stable do you think is version? Tbh I just need jotai Provider and mutableAtom for everything I want to do.

@dai-shi
Copy link
Member

dai-shi commented Jul 4, 2024

valtio v2-rc.0 should be pretty stable but I don't get much feedback.
This PR should be fine. At least, it shouldn't cause any regressions.

@alarbada
Copy link
Author

alarbada commented Jul 4, 2024

Then I'll happily use it. Thanks for your fast change!

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

Successfully merging a pull request may close this issue.

2 participants