Redux Form, Radio Button Fields, how to support variable values?

南楼画角 提交于 2019-12-07 16:50:06

问题


In my react redux form, I have the following:

        <fieldset className="form-group">
          <legend>Radio buttons</legend>
          {this.props.job_titles.map(jobTitle => (
          <div className="form-check" key={jobTitle.id}>
            <label className="form-check-label">
              <Field
                name="job_title_id"
                component="input"
                type="radio"
                value={jobTitle.id}
              />
              {' '}
              {jobTitle.title}
            </label>
          </div>
          ))}
        </fieldset>

This renders the radio buttons correctly, but when you click to select a radio button, the radio button never sets as selected. You can't select an option - the form is broken.

What's strange is if I update: value={jobTitle.id} to value="anything" then the radio buttons can be selected.

I'm not seeing anything in the redux form docs about radio buttons dynamically generated. What am I doing wrong?

Thanks


回答1:


Set the checked property to a state or prop, then update that in the click handler.

<Field
    onClick={
        () => {
            this.setState((prevState) => {
                return {isChecked: !prevState.isChecked};
            });
        }
    }
    name="job_title_id"
    component="input"
    type="radio"
    checked={this.state.isChecked}
    value={jobTitle.id}
/>



回答2:


Convert value to String:

<Field
            name="job_title_id"
            component="input"
            type="radio"
            value={jobTitle.id.toString()}
          />


来源:https://stackoverflow.com/questions/44895103/redux-form-radio-button-fields-how-to-support-variable-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!