Git Product home page Git Product logo

Comments (8)

drcmda avatar drcmda commented on May 3, 2024

Indeed, this has to work.

@JeremyRH It stores the selector as a ref and runs it each render pass over the store-state. Since the function is exchanged, shouldn't it be able to take the updated scope?

from zustand.

drcmda avatar drcmda commented on May 3, 2024

Playing around some more, the store is never updated since nothing in it changes, so it doesn't fire subscribers. setId(id => id + 1) just returns arg0, but doesn't call set. Hence the selector is called only once, initially. Isn't this expected, though?

from zustand.

drcmda avatar drcmda commented on May 3, 2024

On second thought, you're right, when the selector changes it can yield another result, so it should definitively be possible to run a diff even though the store hasn't been changed.

from zustand.

timkindberg avatar timkindberg commented on May 3, 2024

Exactly. We should either add back dependencies or be more restrictive how the selecting can happen. Because if I can write code like this to select state I’m expecting it to work.

from zustand.

drcmda avatar drcmda commented on May 3, 2024

Like this maybe? https://codesandbox.io/s/o7nky5wwj5

This would also work if you remove the [id], in that case it would call the selector on every render, more or less like redux works. If you supply the selector, it runs only on memoize changes. I haven't tested if this causes extra render calls now.

from zustand.

timkindberg avatar timkindberg commented on May 3, 2024

Yeah, you bring up extra renders... I worry that the component will render, triggering the React.useEffect(() => void ping(), deps) which in turn will trigger another render.

from zustand.

timkindberg avatar timkindberg commented on May 3, 2024

I think this seems like a pretty good solution, barring it doesn't cause a bunch of extra renders...

from zustand.

JeremyRH avatar JeremyRH commented on May 3, 2024

I think we can solve this issue without adding the dependencies back. We can run the selector on every call to useStore and return that slice instead of returning the state from React's useState. I'm not sure if this will cause unwanted side effects though. I'll test this out today.

from zustand.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.