Here I have this app that I am working on that generates notes of whatever I type and add in the create area. I tried nested mapping using the following code that generates 3 notes (intended) with each add click.
import React, { useState } from "react";
import Header from "./Header";
import Footer from "./Footer";
import Note from "./Note";
import CreateArea from "./CreateArea";
function App() {
const [notes, setNotes] = useState([]);
function addNote(newNote) {
setNotes(prevNotes => {
return [...prevNotes, newNote];
});
}
function deleteNote(id) {
setNotes(prevNotes => {
return prevNotes.filter((noteItem, index) => {
return index !== id;
});
});
}
return (
<div>
<Header />
<CreateArea onAdd={addNote} />
{notes.map((noteItem, index) => {
return (
<React.Fragment>
<Note
key={index}
id={index}
title={noteItem.title}
content={noteItem.content}
onDelete={deleteNote}
/>
{notes.map((noteItem, index) => {
return (<>
<Note
onAdd={addNote}
key={index}
id={index}
title={noteItem.title}
content={noteItem.content}
onDelete={deleteNote}
/>
{notes.map((noteItem, index) => {
return (
<Note
key={index}
id={index}
title={noteItem.title}
content={noteItem.content}
onDelete={deleteNote}
/>
);
})}</>
);
})}
</React.Fragment>);
})}
<Footer />
</div>
);
}
export default App;
the issue is that when i press add again (second time after already adding 3 notes) instead of adding 3 notes, it adds like 11 new notes (some new and some of the previous added again). you can test it by copying the above code in the App.jsx in this link. https://codesandbox.io/s/keeper-part-3-completed-forked-8zs84v?file=/src/components/App.jsx:0-1628
I want it to add only 3 notes with a single click. (The components have to be nested as shown) I am trying to add an if condition where to stop rendering those multiple extra components but I am stuck.