Redux-Form set component state based on initialValues


Redux-Form set component state based on initialValues



I have a react form that is dynamically generated via data from DB. The issue I'm currently trying to solve is how to enable/disable "child" inputs based on the "parent" input (typically a checkbox). My plan was during rendering of the form I would create component state settings (parentClicked1, parentClicked2, etc) based on initialValues that are populated by redux-form. The name of the component state setting would correspond to the parent input name and would be passed to an onClick method. for example:


const renderSettings = ({ fields, meta: { touched, error, submitFailed } }) => (
<div>
{fields.map((setting, index) => (
<Field
name={`${setting}.value`}
type={fields.get(index).inputType}
component={renderField}
label={fields.get(index).labelText}
format={(value, name) => {
return setBoolValue(value)
}}
onClick={fields.get(index).isParent ? this.onClick(fields.get(index).settingName) : null}
disabled={fields.get(index).isChild ? this.state[fields.get(index).parentSettingName] : null}
/>
))}
</div>
);



The onClick would have something simple like:


onClick(e, stateKey){
this.setState({[stateKey]: e.target.value});
}



The issue is setting the initial value of the component state based on the initialValue populated by redux-form. I tried setting in ComponentDidMount and ComponentDidUpdate but neither appears to get populated. How would I go about doing this?





could you share example on codesandbox.io?
– peeyush singla
2 days ago





Are you just looking for disabling and enabling values of one field on other?
– peeyush singla
2 days ago





@peeyushsingla So this is using the same form that is dynamically generated that you had helped me with in another question. :) I modified it slightly but I'm attempting to disable children inputs associated to a parent. codesandbox.io/s/pm42lmy9lm
– user1015196
2 days ago










By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to input without newline? (Python)

C++ thread error: no type named ‘type’ MINGW

Analog for TagView in flutter