Coder Social home page Coder Social logo

RFC: How is v5-alpha? about zustand HOT 19 OPEN

dai-shi avatar dai-shi commented on May 28, 2024 29
RFC: How is v5-alpha?

from zustand.

Comments (19)

dai-shi avatar dai-shi commented on May 28, 2024 7

5.0.0-alpha.5 is published. 👍 / 👎 / 👀

from zustand.

dai-shi avatar dai-shi commented on May 28, 2024 3

5.0.0-alpha.4 is published. 👍 / 👎 / 👀

from zustand.

dai-shi avatar dai-shi commented on May 28, 2024 3

5.0.0-alpha.6 is published. CJS build was broken in alpha.4 and alpha.5.

from zustand.

jacargentina avatar jacargentina commented on May 28, 2024 2

5.0.0-alpha.6 works fine! Even resolves my react-native described here #1967

from zustand.

dai-shi avatar dai-shi commented on May 28, 2024 2

I'm not sure if some best practices were violated that caused this problem?

Yes, you create a new reference every time in your selector function.

// an easy reproduction
const useFooStore = create((set) => ({
  todos: [],
}))

  // this will cause an infinite loop because of new reference every render
  useFooStore((state) => state.todos.map((todo) => todo.id))

To resolve it, either use createWithEqualityFn & shallow or useShallow.

This infinite-loop behavior can happen without Zustand, and Zustand follows the same behavior.

from zustand.

dai-shi avatar dai-shi commented on May 28, 2024 2

After using reselect, this problem was also solved.

FYI, proxy-memoize also works. https://redux.js.org/usage/deriving-data-selectors#proxy-memoize

from zustand.

charkour avatar charkour commented on May 28, 2024 1

5.0.0-alpha.5 works with [email protected]

from zustand.

dbritto-dev avatar dbritto-dev commented on May 28, 2024 1

#2138

@flq here you go -> #2138

from zustand.

dai-shi avatar dai-shi commented on May 28, 2024 1

Thanks for reporting.
It's an intentional breaking change in #2395.
We would like to add a note in migration guide in docs. Can you create a minimal reproduction to help the docs?

from zustand.

jacargentina avatar jacargentina commented on May 28, 2024

Maybe this ordering on "exports" can be changed for RN users of zustand ?

#1967 (reply in thread)

from zustand.

dbritto-dev avatar dbritto-dev commented on May 28, 2024

Maybe this ordering on "exports" can be changed for RN users of zustand ?

#1967 (reply in thread)

@jacargentina let me take care of that

from zustand.

jacargentina avatar jacargentina commented on May 28, 2024

@dbritto-dev I've solved it by configuring in metro.config.js:

config.resolver.unstable_conditionNames = ['require'];

default is ['require', 'import'] so that import was causing my problem.

from zustand.

dbritto-dev avatar dbritto-dev commented on May 28, 2024

@jacargentina nice, I found that yesterday too but I didn't have time to answer any question. We shall add it to the docs.

from zustand.

flq avatar flq commented on May 28, 2024

Does the readme of the v5 branch already reflect the changes that will be coming up in v5 or is there some other place where one could read about it?

from zustand.

cqh963852 avatar cqh963852 commented on May 28, 2024

5.0.0-alpha.6 fails

5.0.0-alpha.5 fails

image

5.0.0-alpha.4 work fine
image

from zustand.

cqh963852 avatar cqh963852 commented on May 28, 2024

Can you create a minimal reproduction to help the docs?

I will create an example later.


I can first describe how I encountered this problem.

I first migrated from redux toolkit to zustand^4 and everything worked fine.
Then I upgraded [email protected]

The reason for this problem may be that Redux useSelector's callback do'not need to be wrapped by useCallback.

After #2395, which may have caused the issue.

Maybe useSyncExternalStoreWithSelector would be more suitable?

from zustand.

cqh963852 avatar cqh963852 commented on May 28, 2024

https://stackblitz.com/edit/vitejs-vite-qvlgmz?file=src%2Fcomponents%2FTodolist.tsx

@dai-shi I have create a todo list demo with this bug.

The reason why it fails is because of the use of the useTodoFindIds function.

Of course, the actual scenario is more complicated than what is written here. I want to find a set of data and then return their IDs.

I'm not sure if some best practices were violated that caused this problem?

from zustand.

cqh963852 avatar cqh963852 commented on May 28, 2024

After using reselect, this problem was also solved.

image

For RTK users migrating to zustand@next. Maybe be more familiar with reselect.

I think this can be added to the migration manual.

from zustand.

dbritto-dev avatar dbritto-dev commented on May 28, 2024

@flq yeah, I'll add it. btw, they talk about memoizing selectors.

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.