Skip to content

feat(core/hooks): add 'useSet' hook#339

Merged
kimyouknow merged 5 commits into
toss:mainfrom
sukvvon:feat/use-set
Mar 13, 2026
Merged

feat(core/hooks): add 'useSet' hook#339
kimyouknow merged 5 commits into
toss:mainfrom
sukvvon:feat/use-set

Conversation

@sukvvon
Copy link
Copy Markdown
Contributor

@sukvvon sukvvon commented Mar 9, 2026

Overview

Add useSet hook that manages a Set as state, mirroring the useMap pattern.

  • add / remove for adding and removing values
  • toggle adds the value if absent, removes it if present
  • setAll replaces all values in the set
  • reset restores the set to its initial state
  • Accepts Set<T> or T[] as initial state
  • Stable action references via usePreservedCallback
  • SSR-safe, 100% test coverage (16 tests)

Checklist

  • Did you write the test code?
  • Have you run yarn run fix to format and lint the code and docs?
  • Have you run yarn run test:coverage to make sure there is no uncovered line?
  • Did you write the JSDoc?

Copy link
Copy Markdown
Collaborator

@kimyouknow kimyouknow left a comment

Choose a reason for hiding this comment

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

Thanks for addition! useSet mirrors useMap nicely.

Comment on lines +66 to +76
const toggle = usePreservedCallback((value: T) => {
setSet(prev => {
const nextSet = new Set(prev);
if (nextSet.has(value)) {
nextSet.delete(value);
} else {
nextSet.add(value);
}
return nextSet;
});
});
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

One suggestion: the toggle implementation uses if/else — an early
return pattern would be cleaner:

const toggle = usePreservedCallback((value: T) => {
  setSet(prev => {
    const nextSet = new Set(prev);

    if (nextSet.has(value)) {
      nextSet.delete(value);
      return nextSet;
    }

    nextSet.add(value);
    return nextSet;
  });
});

Non-blocking — approved. ✅

@kimyouknow kimyouknow merged commit e5f6cac into toss:main Mar 13, 2026
11 checks passed
@github-actions github-actions Bot mentioned this pull request Mar 13, 2026
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.

2 participants