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

[v5]: follow React "standard" way with breaking behavioral change #2395

Merged
merged 3 commits into from
Mar 9, 2024

Conversation

dai-shi
Copy link
Member

@dai-shi dai-shi commented Mar 8, 2024

For #2138

I think useMemoSelector is a hack that should be avoided. We should generally follow how React works. If we need previous behavior, we should use zustand/traditional which is based on a library provided by React, instead of adding our own hack.

Copy link

vercel bot commented Mar 8, 2024

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

Name Status Preview Comments Updated (UTC)
zustand-demo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 8, 2024 11:53pm

Copy link

codesandbox-ci bot commented Mar 8, 2024

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.

Copy link

github-actions bot commented Mar 8, 2024

Size Change: -90 B (-1%)

Total Size: 6.19 kB

Filename Size Change
dist/react.js 939 B -90 B (-9%)
ℹ️ View Unchanged
Filename Size
dist/index.js 453 B
dist/middleware.js 497 B
dist/middleware/immer.js 566 B
dist/react/shallow.js 817 B
dist/shallow.js 485 B
dist/traditional.js 1.02 kB
dist/vanilla.js 741 B
dist/vanilla/shallow.js 666 B

compressed-size-action

Comment on lines -22 to -26
const { bears, increasePopulation } = useBearStore(
({ bears, increasePopulation }) => ({
bears,
increasePopulation,
}),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say it worked before by chance. This selector creates a new object every time, which isn't supported in v5. We should use a memoized selector, zutand/traditional or 👇 .

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, was this test intentional to check the behavior with such selectors? @TkDodo

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I think I copied this way if creating a store from somewhere. Definitely not intentional

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for confirming. That was the reason I introduced useMemoSelector, without looking at the test code. I believe it is an unnecessary hack.

@@ -474,7 +474,7 @@ it('can set the store without merging', () => {

it('only calls selectors when necessary', async () => {
type State = { a: number; b: number }
const useBoundStore = create<State>(() => ({ a: 0, b: 0 }))
const useBoundStore = createWithEqualityFn<State>(() => ({ a: 0, b: 0 }))
Copy link
Member Author

@dai-shi dai-shi Mar 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behavior (inline selector optimization) isn't supported in v5. Use zustand/traditional.

@dai-shi
Copy link
Member Author

dai-shi commented Mar 8, 2024

While this change may surprise users, v5 should live long with React 19+, so I believe this is a good move. And, major bump is only the chance to make this kind of changes.

@dai-shi dai-shi mentioned this pull request Mar 8, 2024
16 tasks
@dai-shi dai-shi merged commit f87eec5 into v5 Mar 9, 2024
22 checks passed
@dai-shi dai-shi deleted the v5-no-hacks-with-behavioral-change branch March 9, 2024 00:16
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 this pull request may close these issues.

None yet

2 participants