My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Local storage issue

Deactivated User's photo
Deactivated User
·Jan 25, 2020

Hello, I'm working on a notes taking app with local storage but I think I got stuck on this: github.com/matteobarone/notepadjs/tree/loca.. (code below)

import {localStorage} from './local-storage/local-storage';
document.addEventListener('DOMContentLoaded', () => {
const listItemElement = document.querySelector('.notepad__list');
const inputElement = document.querySelector('.notepad__input');
inputElement.addEventListener('keyup', (event) => {
if (event.keyCode === 13) {
addItem(event.target.value, listItemElement);
event.target.value = null;
 }
 })
});
function addItem(text, list, nextNotes) {
const el = document.createElement('li');
const textNode = document.createTextNode(text);
el.appendChild(textNode);
el.classList.add('notepad__item');
list.appendChild(el);
// retrieve the notes that are already in localstorage
const notes = JSON.parse(localStorage.getItem('notes'));
// pushing your new note into the notes array - mutation
notes.push(text);
// setting the notes array JSON into localStorage
localStorage.setItem('notes', JSON.stringify(nextNotes));
}

I am basically trying to retrieve notes from the local storage but all I get is that the previous note is overridden by the next one. I have already looked up online and asked questions on stackoverflow but I don't seem to get to the answer. What am I doing wrong?