question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

Edit on CodeSandbox

Steps to reproduce

选择 Checkbox 任意项如「商品酷」

What is expected?

「品牌返利中心」(value 值为 5)的 Checkbox 被选中。

What is actually happening?

「品牌返利中心」(value 值为 5)的 Checkbox 未被选中。


该需求预期实现CheckboxGroup中选择任意一项,能够控制另一个Checkbox。 开始的时候想可能onChangesetFieldsValue会又触发onChange而导致无限调用onChange,但加了只在某些条件下才setFieldsValue,还是无效。

而为了做对比,新增了Button组件,onClickonChange的逻辑完全一致,onClick是有效的,并且setFieldsValue并不会触发onChange,所以开始的顾虑是不存在的。

这更加让我感到困惑为什么onChangesetFieldsValue不起作用。

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
afc163commented, Apr 25, 2018

别这样做,请仔细阅读 https://ant.design/components/form-cn/#this.props.form.getFieldDecorator(id,-options)

如果需要对 value 做修剪,应该用 normalize

2reactions
yutingzhao1991commented, Apr 25, 2018

因为你 setFieldsValue 的操作会被组件 onChange 之后自己的操作给覆盖掉。

参考 https://codesandbox.io/s/n77367q7wm 这个,加上 setTimeout 可以解决这个问题。

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found