Jest tu funkcja nadpisująca state w którym jest tablica z notakmi
Pojedyncza notka
const note = {
id: new Date().getTime(),
text: "# Text goes Here",
};
Przy próbie zamiany value w note.text dostaję błedy
Cannot read properties of undefined (reading 'id')
Funkcja updateująca note.text
function updateNote(e) {
const { name, value, id } = e.target;
setNotes((oldNotes) =>
oldNotes.map((note) => {
if (note.id === id) return { ...note, [name]: value };
else return note;
})
);
}
Child
<Notes newNote={notes} updateNote={updateNote} />
Notes
function Notes(props) {
const noteElements = props.newNote.map((note, index) => {
return (
<div key={note.id}>
<button key={note.id}>note {index + 1}</button>
<textarea key={note.id + 10} name="text" value={note.text} onChange={props.updateNote}></textarea>
</div>
);
});
return noteElements;
}