localstorage state react with api code example

Example 1: save data in local storage reactjs returns [object, object]

Local Storage is key-value storage where key is string and value is string also.

You must stringify your data, you can do this

localStorage.setItem('user', JSON.stringify(user));


And get it like

const user = JSON.parse(localStorage.getItem('user'));

Example 2: react localstorage

// setter
localStorage.setItem('myData', data);
// getter
localStorage.getItem('myData');
// remove
localStorage.removeItem('myData');
// remove all
localStorage.clear();