Advantages of Kids Coding

Coding programs for kids refers to the opportunities available for children to get involved with computer and improve their math skills, academic performance, become confident problem solvers, etc…


独家优惠奖金 100% 高达 1 BTC + 180 免费旋转

How to take advantage of Local Storage in your React projects

And why you ought to.

Before diving into the tutorial, it may be unclear why you’d want to even use Local Storage in your React apps.

There’s plenty of reasons and use cases, more than I can imagine, but here’s a few I’ve discovered.

Update your App.js with the code below. Here, we’re setting up a simple to-do list app. Absolutely nothing fancy, but it’ll do the trick for playing with localStorage.

After copying over this code, you should be able to add to-do items to the list and remove them.

Saving the value of our newItem input to localStorage is a piece of cake.

Here’s our new updateInput() method.

As you can see, it’s not much different from updating React state.

Open up the Web Developer tools in your browser of choice, find the section for Web Storage (“Application” tab in Chrome), select the current domain of localhost:3000 and watch the value for the newItem key stay in sync with your app’s input.

When adding an item, we save the new, updated list to localStorage and reset the newItem input to a blank string.

Because localStorage can only store strings, arrays and objects need to be passed into JSON.stringify() before being passed to setItem().

Before moving on, we’ll also want to update the list in localStorage when deleting an item.

…the App reverts back to its initial state! We aren’t using the stored items yet, just saving them in the background. Not terribly helpful…

In order to persist the app’s state even after refreshing the page, we need to hydrate the App‘s state with the values in localStorage, with help from a couple new methods:

The below method hydrates the app’s state with the values saved to localStorage. Add this new method to your App component.

Once you’ve added the above code to your App component, refreshing the page no longer resets the app, but keeps it in sync with localStorage!

While our app works as-is by saving React state to localStorage every time the user makes an update, we don’t really need to save so frequently.

Why? Because React keeps track of the state of the app throughout the user’s session — that’s what it’s for! Also, with more complex components and states, it’ll be quite cumbersome and repetitive to use localStorage.setItem() wherever the state is updated.

So rather than continuously keeping localStorage in-sync with React state, let’s simply save state to localStorage whenever the user ends their session, either by leaving the app (‘unmounting’ the component) or refreshing the page.

Alright, here’s a new method for saving all of state to localStorage at once. Add it to your App component.

In order to save state to localStorage when the user leaves the app, we need to invoke the saveStateToLocalStorage method in componentWillUnmount.

Here’s some updated code, where we add the event listener to componentDidMount as well as add what we need to componentWillUnmount.

We no longer need to setItem when updating React state, so you’ll want to remove those.

A lot has changed since the beginning of the tutorial, so here’s the App.js file at this point. *Nothing in the render() method has changed.*

And that’s it! You now have the tools to use Local Storage in your React projects.

Add a comment

Related posts:

Homage to Lance Mannion

I lost a good friend just over a year ago and I’ve been mourning him ever since. He was the author of a marvelous blog with the nom de plume “Lance Mannion” but in real life he was Edwin David…

The Italian Wine Conundrum

There was a time I knew nothing about Italian wine. It feels like yesterday. When I say ‘nothing’ I mean absolutely zero. Less than a single thing. To put it in perspective, I didn’t even know…

Online casino mastercard acceptance

That wide acceptance and built-in trust have made Visa casinos very popular. Most online gambling sites accept Visa as a payment method. Since millions of people have a Visa card in their wallet…