Redux形式的数据绑定无线电字段

时间:2019-05-14 13:34:39

标签: reactjs redux-form

我正在使用redux格式v8.1.0。 db中有一个布尔字段IsCompulsary。 如何使“是”检查为真,如何使“否”检查为假?

    <div>
      <label>
        <Field
          name="IsCompulsary"
          component="input"
          type="radio"              
        />{' '}
        Yes
      </label>
      <label>
        <Field
          name="IsCompulsary"
          component="input"
          type="radio"             
        />{' '}
        No
      </label>
    </div>

更新:

在组件底部:

SchoolSettings = reduxForm({
  form: "schoolSettingsForm", // a unique identifier for this form
  enableReinitialize: true
})(SchoolSettings);
 SchoolSettings = connect(
  state => ({
  initialValues: state.schoolSettings.data // pull initial values from account reducer
})   
)(SchoolSettings);

export default SchoolSettings;

数据为:

{Id: 1, Name: "Our School", IsCompulsary: true}

1 个答案:

答案 0 :(得分:1)

您需要将值字段添加到单选输入并对其值进行规范化,以便在更新商店之前将它们转换为布尔值:

<div>
  <label>
    <Field
      name="IsCompulsary"
      component="input"
      type="radio"
      value={true} 
      normalize={value => value === 'true'}           
    />{' '}
    Yes
  </label>
  <label>
    <Field
      name="IsCompulsary"
      component="input"
      type="radio"    
      value={false}   
      normalize={value => value === 'true'}                    
    />{' '}
    No
  </label>
</div>

检查沙盒https://codesandbox.io/embed/serene-banach-nviml