How to use localStorage with React

How to use localStorage with React

In this article to showcase the usage of session storage and local storage in React to persist your local state, to cache it for browser reloads,

In this article to showcase the usage of session storage and local storage in React to persist your local state, to cache it for browser reloads,

The heart of every React application is its state. Components can have their own internal state and, believe me, despite being simple, you can do a lot of cool stuff with React’s state mechanism. But, let’s say you’re creating a nice app and, at some point, you need to save some user preference locally in the browser or even persist the complete state for one or more of your components. That is, you want to know: How to use localStorage in a React application? Well, setState won’t do this for you, but don’t worry, it’s really simple. Just keep reading!

Web Storage API

Web Storage is an API to store data in the browser, being a significant improvement compared to its predecessor, cookies. It started as a HTML5 API, later evolving to become an independent specification. It’s natively supported by virtually every browser in use today and even some legacy ones, like IE8. Web Storage is implemented in two different ways in browsers: localStorage (a persistent storage, which can be compared to persistent cookies) and sessionStorage (a storage that persists for duration of the session, comparable to session cookies). Though this article focus on localStorage, you can also apply the same principles when using sessionStorage.

Our example component

We need a component to play around with. Well, I think a sign-in page with a “Remember me” option is a perfect fit. For simplicity’s sake, we won’t include a password field, since it shouldn’t be persisted. Let’s check out its render method:

render() {
  return (
    <form onSubmit={this.handleFormSubmit}>
      <label>
        User: <input name="user" value={this.state.user} onChange={this.handleChange}/>
      </label>
      <label>
        <input name="rememberMe" checked={this.state.rememberMe} onChange={this.handleChange} type="checkbox"/> Remember me
      </label>
      <button type="submit">Sign In</button>
    </form>
  );
}

As you can see, it’s a pretty simple form, with two inputs and a button. The code needed to handle its state is equally easy to understand:

export default class SignIn extends Component {
  state = {
    user: '',
    rememberMe: false
  };

  handleChange = (event) => {
    const input = event.target;
    const value = input.type === 'checkbox' ? input.checked : input.value;

    this.setState({ [input.name]: value });
  };

  handleFormSubmit = () => {};

  render() { /*...*/ }
}

In the above piece of code, we’re using the setState method to keep the component’s state up-to-date every time a change occurs in our form. If you run the project, here’s what you’ll get:

Note: I know its appearance is raw, but for the purpose of this article, let’s turn a blind eye to this. =)

If you fill the User input, then check the “Remember me” option and finally click the “Sign in” button, you’ll notice that the page is reloaded and the form becomes empty again:

This behavior is completely normal. Now, we will learn how to use localStorage with React and make this form work as expected.

Step 1: Saving data in the localStorage

We have two things to be stored in our example:

  1. The value of the “Remember me” option
  2. The user name (only if the “Remember me” option is checked)

The perfect place to persist these values is in the handleFormSubmit method:

handleFormSubmit = () => {
  const { user, rememberMe } = this.state;
  localStorage.setItem('rememberMe', rememberMe);
  localStorage.setItem('user', rememberMe ? user : '');
};

So, what is really happening in this method?

  • First, we use destructuring assignment to initialize two variables
  • Then, we’re calling the localStorage.setItem method to store the value of the “Remember me” option
  • Finally, we persist the user name too, but only if the “Remember me” option is set to true

And this is what we have so far:

Ok, we’re halfway there. Now we need to get back those values to rehydrate our component.

Step 2: Retrieving data from the localStorage

We’ll put our data retrieval logic in the componentDidMount lifecycle method, but you can safely put it in your constructor, if you want to, since its purpose is to set the initial state of our component.

Note: You can’t call setState before the component is mounted, so, if you prefer the constructor approach, just set the state property directly instead. As for me, I like to keep my constructors as clean as possible, but it’s a matter of personal preference.

componentDidMount() {
  const rememberMe = localStorage.getItem('rememberMe') === 'true';
  const user = rememberMe ? localStorage.getItem('user') : '';
  this.setState({ user, rememberMe });
}

Let’s understand it better:

  • First, we use destructuring assignment to initialize two variables
  • Then, we’re calling the localStorage.setItem method to store the value of the “Remember me” option
  • Finally, we persist the user name too, but only if the “Remember me” option is set to true

And here’s the magic happening:

Great! Everything worked as expected. Take a look at the final code, here.

A final tip

As I mentioned above, localStorage can only store data as string. If you’re dealing with just a few persisted values, this is no big deal. But if you intend to make a significant use of localStorage throughout your application, I totally recommend you to pick a library to make it easier to save and retrieve data and to abstract things like stringification and parsing. An excellent and battle-tested option is Store.js. That’s my personal recommendation. =)

Conclusion

  • First, we use destructuring assignment to initialize two variables
  • Then, we’re calling the localStorage.setItem method to store the value of the “Remember me” option
  • Finally, we persist the user name too, but only if the “Remember me” option is set to true

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Why ReactJS is better for Web Application Development?

Web Application Development is the point of contact for a business in today's digital era. It is important to choose the right platform for Web Application Development to build a high end Web

ReactJS Development Services | ReactJS Development Company - Chapter 247 Infotech

Chapter 247 Infotech is a leading ReactJS development company in India, USA, offering ReactJS development services at par to a spectrum of business domains from E-commerce, healthcare to Edutech at

Pagination in ReactJs

There are a lot of resourceful materials online that give good insights into pagination in ReactJs, as well as NPM packages you can easily use