I came to a point where I move really comfortable within Vanilla JS when it comes to Procedural programming, so I jumped into Functional programming to widen my knowledge and get ready for the next level of Javascript usage...
Well, it took like 3 minutes to get to my first unwanted encounter were saving to the local store not exactly working as I planned.
The app would simply add userInput.value
to the local-storage when the button is clicked.
What happens is it adds to the empty local-store. Fine.
Then when the user tries to add again this is the result
.
Here is the full code, there is only one userInput.value
and a button
to trigger
//////////////////////////////////////////////////////
// Functional programmed version:
//////////////////////////////////////////////////////
function getStorageData() {
return localStorage.getItem('itemsArray') || [];
}
function setStorageData(obj) {
return localStorage.setItem('itemsArray', JSON.stringify(obj));
}
function getUserInput(input) {
return document.querySelector(input).value;
}
function addToStorage(userInput) {
return setStorageData({...getStorageData(), userInput});
}
function submitHandler(event) {
event.preventDefault();
addToStorage(getUserInput('input'));
}
function connectForm(button, submit) {
const addBtn = document.querySelector(button);
addBtn.addEventListener('click', submit);
}
connectForm('.add-btn', submitHandler)
A problem I see the app tries to put it as an object like {userInput: value}
instead just value
so it probably breaking on adding the same object with a different value... How can I get rid of the variable here? It seemingly puts the variable there automatically.
Thanks for all help in advance!