I am trying to call handleAddNote which is passed from App.js to AddNote.js but I am not able to reference it in AddNote without getting error saying handleAddNote is not a function. I have tried multiple ways but I always get it is not a function or is not defined. In the tutorial I'm following it says to simply call handleAddNote(noteText) in handleSave function but I am not able to.
import Note from './Components/Note';
import NotesList from './Components/NotesList';
import { useState } from 'react';
import { nanoid } from 'nanoid';
const App = () => {
const[notes, setNotes] = useState([
{
id: nanoid(),
text: "My first note",
date: "2/12/2024"
},
{
id: nanoid(),
text: "My second note",
date: "2/13/2024"
},
{
id: nanoid(),
text: "My third note",
date: "2/14/2024"
},
{
id: nanoid(),
text: "My fourth note",
date: "2/15/2024"
}])
const addNote = (text) => {
console.log(text);
}
return (
<div className="container">
<NotesList notes={notes} handleAddNote={addNote}/>
</div>
);
}
export default App;
import Note from './Note';
import AddNote from './AddNote';
const NotesList = ({ notes, handleAddNote }) => {
return (
<div className="notes-list">
{notes.map((note)=>
<Note id={note.id} text={note.text} date={note.date}/>
)}
<AddNote handleAddNote={handleAddNote}/>
</div>
);
}
export default NotesList;
import { useState } from 'react';
const AddNote = ({ handleAddNote }) => {
const [noteText, setNoteText] = useState("");
const handleChange = (event) => {
setNoteText(event.target.value);
};
const handleSave = () => {
console.log({handleAddNote});
{handleAddNote(noteText)};
};
return(
<div className="note new">
<textarea
onChange={handleChange}
value={noteText}
className='new-note'
rows="8"
columns="10"
placeholder="Type to add a note...">
</textarea>
<div className="note-footer">
<small>200 remaining</small>
<button className='save' onClick={(noteText)=>handleAddNote(noteText)}>Save</button>
</div>
</div>
);
}
export default AddNote
In the above code, it looks like the function is passed correctly to the child component.
Remove the curly braces
{handleAddNote(noteText)}fromhandleSavefunction.Just use
handleAddNote(noteText)and it should work.