Ad
How To Store Token In LocalStorage In React Library
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
Related Questions
- → Import statement and Babel
- → should I choose reactjs+f7 or f7+vue.js?
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → .tsx webpack compile fails: Unexpected token <
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → React Native with visual studio 2015 IDE
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
- → How do I determine if a new ReactJS session and/or Browser session has started?
- → Alt @decorators in React-Native
- → How to dynamically add class to parent div of focused input field?
Ad