setFieldsValue not work in Checkbox onChange event
See original GitHub issue- I have searched the issues of this repository and believe that this is not a duplicate.
Version
3.4.3
Environment
Mac Chrome 版本 66.0.3359.117(正式版本)
Reproduction link
Steps to reproduce
选择 Checkbox 任意项如「商品酷」
What is expected?
「品牌返利中心」(value 值为 5)的 Checkbox 被选中。
What is actually happening?
「品牌返利中心」(value 值为 5)的 Checkbox 未被选中。
该需求预期实现CheckboxGroup
中选择任意一项,能够控制另一个Checkbox
。
开始的时候想可能onChange
内setFieldsValue
会又触发onChange
而导致无限调用onChange
,但加了只在某些条件下才setFieldsValue
,还是无效。
而为了做对比,新增了Button
组件,onClick
和onChange
的逻辑完全一致,onClick
是有效的,并且setFieldsValue
并不会触发onChange
,所以开始的顾虑是不存在的。
这更加让我感到困惑为什么onChange
内setFieldsValue
不起作用。
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Antd form setFieldValue on OnChange event is not working
You are loosing the value being set because the onChange for Antd forms are async and are being run after the onChange that...
Read more >Antd form setFieldValue on OnChange event is not working ...
Coding example for the question Antd form setFieldValue on OnChange event is not working-Reactjs.
Read more >Form - Ant Design
setFieldsValue do not trigger onFieldsChange or onValuesChange ? It's by design. Only user interactive can trigger the change event. This design is aim...
Read more >How To Fix It Cannot Use 'Setfieldsvalue' - ADocLib
After ckick some checkbox, try to click all check is not work react navigation ... onChange doesn't fire if input re-renders due to...
Read more >useForm - setValue - React Hook Form
This function allows you to dynamically set the value of a registered field and ... doesn't create new input setValue('test.101.data') // ✓ work...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
别这样做,请仔细阅读 https://ant.design/components/form-cn/#this.props.form.getFieldDecorator(id,-options)
如果需要对 value 做修剪,应该用
normalize
。因为你
setFieldsValue
的操作会被组件 onChange 之后自己的操作给覆盖掉。参考 https://codesandbox.io/s/n77367q7wm 这个,加上 setTimeout 可以解决这个问题。