Skip to content

Commit 04dd6b9

Browse files
committedFeb 8, 2024
adicao do localStorage para salvar as listas no browser
1 parent bc8e9e2 commit 04dd6b9

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed
 

‎src/app.tsx

+14-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ interface Note{
1010
};
1111

1212
export function App() {
13-
const [notes, setNotes] = useState<Note[]>([]);
13+
const [notes, setNotes] = useState<Note[]>(() => {
14+
const notesOnStorage = localStorage.getItem("notes");
15+
16+
if(notesOnStorage){
17+
return JSON.parse(notesOnStorage);
18+
}
19+
20+
return [];
21+
});
1422

1523
function onNoteCreated(content: string){
1624
const newNote = {
@@ -19,7 +27,11 @@ export function App() {
1927
content
2028
};
2129

22-
setNotes([newNote, ...notes]);
30+
const notesList = [newNote, ...notes];
31+
32+
setNotes(notesList);
33+
34+
localStorage.setItem("notes", JSON.stringify(notesList));
2335
}
2436

2537
return (

0 commit comments

Comments
 (0)