简体   繁体   English

React state 和单选按钮上的 initialState 的奇怪行为... [已解决原因是<validatedform> react-jhispter 以 defaultValues 失败]</validatedform>

[英]Strange behavior with React state and initialState on radio button... [SOLVED cause is <ValidatedForm> react-jhispter failed with defaultValues]

I have a strange behavior that I can't solve using State to change Radio button.我有一个奇怪的行为,使用 State 更改单选按钮无法解决。

I open my page, 'Years' is selected.我打开我的页面,选择了“年”。 It is correct.它是正确的。 We can also see the state property in the above text field (it contains 'years');我们还可以在上面的文本字段中看到 state 属性(它包含“年”);

开幕

If I click on 'All', the state changes (contains 'all') BUT the RADIO button is not checked.如果我单击“全部”,state 会发生变化(包含“全部”),但未选中 RADIO 按钮。

在此处输入图像描述

If I CLICK AGAIN on the same RADIO BUTTON 'All', it shows correctly:如果我再次点击同一个单选按钮“全部”,它会正确显示:

在此处输入图像描述

It is like onCLick works (second time), but onCHange not.就像 onCLick 工作(第二次),但 onCHange 没有。 It is like after running my onCHange function, it resets to the initial state on the page (checking the 'years' radio).就像在运行我的 onCHange function 之后,它重置为页面上的初始 state(检查“年”收音机)。

I have not clue.我不知道。

Here is some part of my code (the minimum to understand):这是我的代码的一部分(至少要理解):

<label><input name='choix' type='radio' value='all' onChange={(e) => choixChanged(e)} checked={radioChoiceState === "all"} /> All</label>

<label><input name='choix' type='radio' value='dateRange' onChange={choixChanged} checked={radioChoiceState === "dateRange"} /> Date range</label>

<label><input name='choix' type='radio' value='years' onChange={choixChanged} checked={radioChoiceState === 'years'} /> Only years</label>

... truncated ...

const [datesRangeVisibleState, storeDatesRangeVisible] = useState(false);
const [radioChoiceState, storeRadioChoiceState] = useState('years');


const choixChanged = event => {
    const butt = event.target.value;
if (butt === 'all') {
  storeDatesRangeVisible(false);
} else if (butt === 'dateRange') {
  storeDatesRangeVisible(true);
} else {
  storeDatesRangeVisible(false);
}
storeRadioChoiceState(butt);
}

Please help me...请帮我...

OKAY.. I finally FOUND the root cause.. which is the <ValidatedForm> from react-jhispter that surrounds all the 3 radio buttons.. It FAILS if I add the attribute: defaultValues=....好的..我终于找到了根本原因..这是来自 react-jhispter 的 <ValidatedForm> 围绕所有 3 个单选按钮..如果我添加属性会失败:defaultValues=....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM