changeHandler = (value, controlName, controls) => {
const formControls = controls.slice();
const control = formControls[controlName]
control.touched = true
control.value = value
control.valid = validate(control.value, control.validation)
formControls[controlName] = control
this.setState({
})
}
this.setState({
formcontrols,
isFormValid: validateForm()
})
changeHandler = (value, controlName, controls) => {
const formControls = controls.slice();
const control = formControls[controlName]
control.touched = true
control.value = value
control.valid = validate(control.value, control.validation)
formControls[controlName] = control
this.setState({
formcontrols,
isFormValid: validateForm()
})
}
renderControls(controls) {
return controls.map((control,index) => {
return(
<Auxillary key={index}>
<Input
type={control.type}
label={control.label}
value={control.value}
valid={control.valid}
shouldValidate={!!control.validation}
touched={control.touched}
errorMessage={control.errorMessage}
onChange={event => this.changeHandler(event.target.value, index, controls)}
/>
</Auxillary>
)
})
}
Это вызываю в render-e
{this.renderControls(this.state.enrollerControls)}
const formControls = controls.slice();
{this.renderControls(this.state.enrollerControls)}
renderControls(controls) {
return controls.map((control, index) => {
return (
<Auxillary key={index}>
<Input
type={control.type}
label={control.label}
value={control.value}
valid={control.valid}
shouldValidate={!!control.validation}
touched={control.touched}
errorMessage={control.errorMessage}
onChange={event =>
this.changeHandler(event.target.value, index, controls)
}
/>
</Auxillary>
);
});
}
changeHandler = (value, controlName, controls) => {
console.log("controls", controls);
const formControls = controls.slice();
console.log("controls", controls);
const control = formControls[controlName];
// А ВОТ ТУТ ВЫ МУТИРУТЕ СТЕЙТ ИЗМЕНЯЯ ЕГО ЗНАЧЕНИЕ НА ПРЯМУЮ БЕЗ ИСПОЛЬЗОВАНИЯ SET STATE
control.touched = true;
control.value = value;
control.valid = validate(control.value, control.validation);
console.log(formControls[0]);
formControls[controlName] = control;
this.setState({
isFormValid: validateForm()
});
};
const formControls = controls.slice();
console.log(controls == formcontrols)