I'm trying to do a really basic todo app in React using local storage, and am using an array of objects for my data. This seems to work fine for loading information up, but no matter what I do I can't seem to update local storage.
I started backtracking and found that after my handleSave
function, if I console log this.state.todoData
I get the correct, updated values. If, immediately after that console log, I try logging JSON.stringify(this.state.todoData)
it brings me back the old data in stringified form, rather than the updated data. This is even before I do anything with local storage.
I can't for the life of me understand why console logging this.state.todoData
vs JSON.stringify(this.state.todoData)
would bring me a different data set.
Here is my handleSave function:
handleSave (idNum, title, due, description) {
this.setState(prevState => ({
todoData: prevState.todoData.map(obj =>
obj.id === idNum
? Object.assign(obj, {
inEditMode: false,
title: title,
due: due,
description: description
})
: obj
)
}))
console.log(this.state.todoData)
console.log(JSON.stringify(this.state.todoData))
}