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

Fix: respect defaultValue in Select and MultiSelect #522

Merged
merged 5 commits into from
Jan 20, 2025

Conversation

ariser
Copy link
Collaborator

@ariser ariser commented Jan 17, 2025

Why

Currently, defaultValue in Select and MultiSelect doesn't work. The reason is the effect that tracks updates of the value prop. It runs on mount as well, clearing the state with selected values that should contain default value.

What

  • introduced a new hook, useUpdateEffect, that works exactly as componentDidUpdate in classical component lifecycle, i.e. runs every update, but not on the first render
  • for tracking value props update, changed useEffect in Select and MultiSelect to useUpdateEffect
  • covered the basic behavior of defaultValue with test cases

@ariser ariser requested review from serdec and vineethasok January 17, 2025 18:35
Copy link

vercel bot commented Jan 17, 2025

You must have Developer access to commit code to ClickHouse on Vercel. If you contact an administrator and receive Developer access, commit again to see your changes.

Learn more: https://vercel.com/docs/accounts/team-members-and-roles/access-roles#team-level-roles

@@ -0,0 +1,15 @@
import { useEffect, useRef } from "react";

export const useUpdateEffect: typeof useEffect = (effect, deps) => {
Copy link
Collaborator Author

@ariser ariser Jan 17, 2025

Choose a reason for hiding this comment

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

@vineethasok there are no custom hooks in click-ui as of now, so this is a vital point.
I find both custom hooks, and useUpdateEffect in particular, to be quite useful in certain cases. WDYT? Are you ok with adding a new module with hooks?

I can find other ways to deal with the bug, but in my mind running this effect on updates only is exactly the way to fix it.

@ariser ariser merged commit bf10971 into main Jan 20, 2025
4 of 5 checks passed
@ariser ariser deleted the fix-select-default-val branch January 20, 2025 12:04
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.

3 participants