const initialStateInput = { cabeceraFamilia: { familia: '', direccion: '', telefonos: '', email: '' }, motivoConsulta: '', fechaHora: '', corresponsables: [], }
const [infoAgendamiento, setInfoAgendamiento] = useState(initialStateInput);
const actualizarState = e => { const nameObjects = e.target.name.split('.'); const newState = setStateNested(infoAgendamiento, nameObjects, e.target.value); setInfoAgendamiento({...newState}); };
const setStateNested = (state, nameObjects, value) => { let i = 0; let operativeState = state; if(nameObjects.length > 1){ for (i = 0; i < nameObjects.length - 1; i++) { operativeState = operativeState[nameObjects[i]]; } } operativeState[nameObjects[i]] = value; return state; }
< input type="text" className="form-control" name="cabeceraFamilia.direccion" placeholder="Dirección" defaultValue={infoAgendamiento.cabeceraFamilia.direccion} onChange={actualizarState} / >
Sorry, my English is not the best but i wanted to share my solution ... your article helped me for it
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
const initialStateInput = {
cabeceraFamilia: {
familia: '',
direccion: '',
telefonos: '',
email: ''
},
motivoConsulta: '',
fechaHora: '',
corresponsables: [],
}
const [infoAgendamiento, setInfoAgendamiento] = useState(initialStateInput);
const actualizarState = e => {
const nameObjects = e.target.name.split('.');
const newState = setStateNested(infoAgendamiento, nameObjects, e.target.value);
setInfoAgendamiento({...newState});
};
const setStateNested = (state, nameObjects, value) => {
let i = 0;
let operativeState = state;
if(nameObjects.length > 1){
for (i = 0; i < nameObjects.length - 1; i++) {
operativeState = operativeState[nameObjects[i]];
}
}
operativeState[nameObjects[i]] = value;
return state;
}
< input type="text" className="form-control" name="cabeceraFamilia.direccion" placeholder="Dirección" defaultValue={infoAgendamiento.cabeceraFamilia.direccion} onChange={actualizarState} / >
Sorry, my English is not the best but i wanted to share my solution ... your article helped me for it