Tiene problemas para actualizar un campo cuyo valor está dentro de un objeto dentro de un objeto dentro de una matriz:
[ { animals: { an1: 'lynx', an2: 'tiger' }, location: 'asia' }, { animals: { an1: 'pigeon', an2: 'eagle' }, location: 'europe' }, ]; const handleChange = (e, input) => { let updatedList = data.map((item, i) => { if (i === index) { const allAnimals = { ...item.animals }; allAnimals[input] = e.target.value; return { ...item, animals: allAnimals }; } return item; }); setData(updatedList); } ¿Cómo actualizaría an1 o an2 en el objeto animals ?
Si simplemente desea reemplazar todos los índices, lo siguiente debería funcionar:
const data = [ { animals: { an1: 'lynx', an2: 'tiger' }, location: 'asia' }, { animals: { an1: 'pigeon', an2: 'eagle' }, location: 'europe' }, ]; const handleChange = (e, input) => data.map(it => ({ ...it, animals: { ...it.animals, [input]: e.target.value, } })) console.log(handleChange({ target: { value: 'test', }}, 'an2' ))Tenga en cuenta que esto reemplazará/agregará el nuevo valor a cada matriz.