pmndrs / valtio

💊 Valtio makes proxy-state simple for React and Vanilla
http://valtio.pmnd.rs
MIT License
8.67k stars 241 forks source link

replace listeners on an object that is replaced #875

Closed Tucker-Eric closed 3 months ago

Tucker-Eric commented 3 months ago

Related Issues or Discussions

Relates to #449

Summary

This change allows for listeners to be resubscribed to objects that are replaced.

These changes allow the following code to not lose the subscription to the object that previously would lose the subscription:

const unsubscribe = subscribe(state.myObject, () => {
  // callback to run whenever object changes
  if(needToReplace) {
    state.myObject = newObjectState
  }
})

Check List

vercel[bot] commented 3 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
valtio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 27, 2024 3:29pm
codesandbox-ci[bot] commented 3 months ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

dai-shi commented 3 months ago

Thanks for the suggestion. However, I think this is by design. We want the implementation to be minimal and I don't think this change works for some edge cases. For example, we still want to use the old object.