Ad

How To Store Token In LocalStorage In React Library

- 1 answer

I am very new to react, I have written everything code in the backend. Now if I log in now token is coming into the network. Now my problem is how to store that token in LocalStorage and where I have to store it, please tell me what to do and how to do.

If you have any doubts, please put a comment

Ad

Answer

You can store it as a client-side cookie or in a localStorage or sessionStorage. There are pros and cons in each option. you should have access to the localStorage instance which has setter and getter to store and retrieve data from the local storage.

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

You can also check this snippet for reference. https://codesandbox.io/embed/winter-bird-uirri?fontsize=14&hidenavigation=1&theme=dark

Ad
source: stackoverflow.com
Ad