hooks icon indicating copy to clipboard operation
hooks copied to clipboard

fix: setState should emit state update

Open leezng opened this issue 4 years ago • 3 comments

使用setState更新时,如果前后query并无变化,此时state并不会更新,为了与useState更类似,这里在setState时,不管数据是否有变化,state都应触发改变

🤔 This is a ...

  • [ ] New feature
  • [x] Bug fix
  • [ ] Site / documentation update
  • [ ] Demo update
  • [ ] TypeScript definition update
  • [ ] Bundle size optimization
  • [ ] Performance optimization
  • [ ] Enhancement feature
  • [ ] Internationalization
  • [ ] Refactoring
  • [ ] Code style optimization
  • [ ] Test Case
  • [ ] Branch merge
  • [ ] Other (about what?)

🔗 Related issue link

#881 #888

💡 Background and solution

📝 Changelog

Language Changelog
🇺🇸 English
🇨🇳 Chinese

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • [ ] Doc is updated/provided or not needed
  • [ ] Demo is updated/provided or not needed
  • [ ] TypeScript definition is updated/provided or not needed
  • [ ] Changelog is provided or not needed

leezng avatar Dec 10 '21 09:12 leezng

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

CLAassistant avatar Dec 10 '21 09:12 CLAassistant

没意会到这么改有啥好处。。

brickspert avatar Dec 13 '21 02:12 brickspert

const [options, setOptions] = useUrlState({
  key: '',
  key2: '',
  key3: '',
})

useRequest({
...
}, {
  refreshDeps: [options]
})

这样一个场景,request通过options驱动,用户在页面上的操作触发了筛选条件变化,可能key从1变到2,这在现有的逻辑上是正常的,但是如果用户的操作没有导致key的变化,例如只是一个搜索框,按下回车,这时候筛选条件的值还是原来的options,这里的setOptions就不会改变到原来的对象,如果换成useState+useEffect的写法,如下:

const [options, setOptions] = useState({})

useEffect(() => {
  getData()
}, [options])

这里的每一次setOptionts都是应该会去触发数据刷新的

leezng avatar Dec 13 '21 03:12 leezng

const [options, setOptions] = useUrlState({
  key: '',
  key2: '',
  key3: '',
})

useRequest({
...
}, {
  refreshDeps: [options]
})

这样一个场景,request通过options驱动,用户在页面上的操作触发了筛选条件变化,可能key从1变到2,这在现有的逻辑上是正常的,但是如果用户的操作没有导致key的变化,例如只是一个搜索框,按下回车,这时候筛选条件的值还是原来的options,这里的setOptions就不会改变到原来的对象,如果换成useState+useEffect的写法,如下:

const [options, setOptions] = useState({})

useEffect(() => {
  getData()
}, [options])

这里的每一次setOptionts都是应该会去触发数据刷新的

以我的理解,你这是想实现,搜索框里的内容没变时,还要触发搜索。这个行为你可以通过在搜索的回调中,手动调用 useRequest 返回的 run 方法来实现。

其次,这个 PR 的代码已经过时了,master 分支上的代码已经不适用这个 PR 的改动了(不仅仅是代码冲突问题)。所以 PR 关了

liuyib avatar Mar 11 '23 09:03 liuyib