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

feat: [useMergedState] inject prevState to postState(next,prev) #450

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vaynevayne
Copy link

修改前,
可否修改的拦截器(autoClearSearchValue === true ) 需要在每一个操作前设置, 这种 case by case 的操作对人的心智负担很大, 容易出现遗漏

 const [mergedSearchValue, setSearchValue] = useMergedState('', {
      value: searchValue !== undefined ? searchValue : inputValue,
      postState: (search) => search || '',
    });
   if (formatted) {
          const newRawValues = Array.from(new Set<RawValueType>([...rawValues, formatted]));
          triggerChange(newRawValues);
          triggerSelect(formatted, true);
          if(autoClearSearchValue) setSearchValue('');
          
        }

修改后, 把拦截器放到整个useMergedState 里, 这样无论在哪里调用,都可以走拦截判断, 可以大大减少bug数

 const [mergedSearchValue, setSearchValue] = useMergedState('', {
      value: searchValue !== undefined ? searchValue : inputValue,
      postState: (search,prevSearch) => (autoClearSearchValue ? search: prevSearch) || '',
    });
   if (formatted) {
          const newRawValues = Array.from(new Set<RawValueType>([...rawValues, formatted]));
          triggerChange(newRawValues);
          triggerSelect(formatted, true);
         setSearchValue('');
        }

目的,减少组件bug,

inject prevState to  postState(next,prev)
@vercel
Copy link

vercel bot commented Jun 10, 2023

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

Name Status Preview Comments Updated (UTC)
util ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 10, 2023 5:44am

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.

1 participant