- Overview
- Transcript
4.5 Add `localStorage` Support
Right now, our application state is erased every time we refresh the page. We don’t want that to happen, so we’ll create a temporary fix: a little bit of code to save our state in Window.localStorage
.
Related Links
1.Introduction2 lessons, 04:39
1.1Introduction01:31
1.2Application Demo03:08
2.Get Started With Redux6 lessons, 41:43
2.1Set Up the Project09:34
2.2Reducers and Actions10:10
2.3Combining Reducers08:42
2.4Challenge: Add a Case to a Reducer04:44
2.5Challenge: Split a Reducer05:19
2.6Challenge: Build a Component03:14
3.Create React Components8 lessons, 50:18
3.1Build a Pure Component04:53
3.2Start the Sidebar04:31
3.3Write Action Creators08:37
3.4Use Action Creators06:42
3.5Challenge: Temperature Converter05:50
3.6Challenge: Todo List09:21
3.7Challenge: Action Creators07:26
3.8Challenge: Refs Research02:58
4.Application Structure9 lessons, 56:09
4.1Refactor Our Application for Growth08:43
4.2Using the `react-redux` Package13:12
4.3Add a Router07:24
4.4Create Nested Routes07:44
4.5Add `localStorage` Support03:38
4.6Challenge: Presentational and Container Components07:26
4.7Challenge: Basic Routing02:53
4.8Challenge: Route Not Found02:51
4.9Challenge: Route Parameters02:18
5.Implement the App9 lessons, 1:31:34
5.1Create the Toolbar06:16
5.2Create the New Card Modal15:16
5.3Display a Deck of Cards05:27
5.4Create the Edit Card Modal10:20
5.5Filter Cards06:24
5.6Create a Study Interface19:29
5.7Add Asynchronous Actions13:08
5.8Challenge: General Conversion Component07:11
5.9Challenge: Users List Component08:03
6.Conclusion1 lesson, 01:32
6.1Conclusion01:32
4.5 Add `localStorage` Support
This lesson will have pretty much nothing to do with react or redux. However, it is going to solve the problem that we're going to have moving forward in the development of our application. And that is that right now, every time we refresh the page we have to start our state from the scratch. So we have no decks. So if I go ahead and created a deck, I can display the deck and eventually I'll be able to create cards and study them. However, every time I refresh the page the deck disappears. So how can we fix this problem? Well, the quickest and easiest way right now is to just store our state in local storage. Then there's no need if we do this with redux so let's create a microlibrary that will do this for us. I'm gonna create a new file called src/localStore.js. And in here, we're just gonna export two functions. So let's say, export const getLocalStore and this is going to be a function that simply does JSON.parse. And we're going to parse localStorage.getItem state. Now if localStorage.getItem is empty or return null, JSON.parse. Will return null then and we don't want null, we want undefined. So, let's say or undefined. So that way, if we don't have any state yet we'll just return undefined. Next, let's export another constant which will be setLocalStore and this function will take two parameters. The first one will be the actual state that we want to save, and the second one will be the specific properties from that state that we want to save. Cuz we don't wanna save everything, we don't wanna save the UI or the router state. Instead, we just wanna save the data. Which in our case, will be decks and cards. So what we can do now is let's create a new object called toSave, and that will just be an empty object. And then lets loop over props so we can say, props.forEach, and forEach prop what will do is will set that prop on to save equal to whatever that prop is on the state. And then we can just finish off by saying localStorage.setItem and we can set the state to be JSON.stringify, and will pass in to save. So now, if we come back to our App.js file. Let's go ahead and import star, as we'll just call localStore from localStore. Then down here on line fifteen, where we create our store, we can actually pass an object of state, as the second parameter to store. So let's say, localStore.getLocalStore, and you know what, that function name is too long, let's just change it to get and set. So, if we come back here, we can have localStore.get an localStore.set. That makes much more sense. Okay, so, we have localStore.get. Now, within our run function, here, let's go ahead and actually save this. We're getting our state here, so let's call localStore.set. We'll pass at the set, and we'll pass at an array which has decks and cards. And those are the only two values we care about. That's really all we need to do. We just need to save the state any time it changes and whenever we refresh the page. Get that save state from our local storage. So let's give this a try. If we come back to the browser, let's create a new deck called, one, create a new deck called, two. Refresh the page and you can see that they're both still there. We can actually see our state in the local storage. If we do localStorage.getItem and let's get state, and you can see that we have a deck named one and a deck name two, and an empty cards array. Excellent, so now as we go forward and add more features to this project, we don't have to recreate the stage every time we want to test a new feature.