I'm trying to implement a notes app with a title
property. I, previously, had the notes app working w/o adding-a-title functionality but now that I've integrated it, I have the following issue: the return of the textarea
element (the note text itself) is undefined while the return of the input
element (the note title) returns the valid title.
The code below is, I'm certain, the component where the error stems from - the component being AddNote
.
What I've tried thus far:
- Using a callback, in reference to closure hell
- Combining the states
- Tracing the error with React Dev Tools on Chrome
What I haven't tried:
import React, { useState } from 'react';
const AddNote = ({ handleAddNote }) => {
const [noteTitle, setNoteTitle] = useState('');
const [noteText, setNoteText] = useState('');
const handleSaveClick = () => {
if ((noteText.trim().length > 0) && (noteTitle.trim().length > 0)) {
handleAddNote(noteText);
setNoteText('');
handleAddNote(noteTitle);
setNoteTitle('');
}
};
return (
<div className='note new'>
<div className="toolbar">
<button
className='add-delete-icon'
onClick={handleSaveClick}
>+</button>
<input
type="text"
className="note-title"
placeholder="Add a title..."
value={noteTitle}
onChange={
event => setNoteTitle(event.target.value)
}
autoFocus
/>
</div>
<textarea
className="note-text"
placeholder="Add an idea..."
value={noteText}
onChange={
event => setNoteText(event.target.value)
}
></textarea>
</div>
);
};
export default AddNote;
Am I doing something incorrectly here, particularly with using multiple instances of useState
, i.e., this idea of "batching" the 'setState' calls? Or rather, does the issue fall within the handleSaveClick
anonymous function?
The dependent components - meaning, the one's that refer to adding-a-title-functionality - are the Note
component (Pastebin), the NoteList
component (Pastebin), and notable the Home
component (Pastebin).