React Context & Hooks Tutorial #21 - Adding Local Storage

Net Ninja · Beginner ·🌐 Frontend Engineering ·6y ago

Key Takeaways

Uses React Context API with Hooks to create a reading list app with local storage and reducers

Full Transcript

okay then gang so there's one last piece to the puzzle here that I want to add in and that is local storage because right now if I was to add some kind of book title and an author if I then refresh I lose that data and we don't see there anymore now I will like that data to persist and to do that we're going to use local storage which is a way for us to store data locally in the users browser so if you head on over to the applications app you might have to click this arrow to see it and then go to local storage you can see we have a local storage for this web address right here and all local storage is is key and value pairs so we can set a key and then a value much like a JavaScript object but this value has to be some kind of string so I'll give you a quick demo what I'm gonna do is first of all just say a local storage and press Enter this is how we access the local storage API in a browser so if I press ENTER then I can see currently it has a length of 0 there's nothing in it whatsoever but also if we open it and go to proto we can see all these different methods so one of them is set item and that's how we add an item to local storage and another is get item and that's how we get an item from local storage so these are the two methods we need one to basically save an item there and want to retrieve it so for example if I say local storage dots set item and then I'm just going to create an item called name and then the second parameter is going to be the value I'm just gonna say Shawn and press Enter and that is going to create a key of name and a value of Shawn so if we go to local storage now we can see we have a key of name and a value of Shawn and now if I wanted to get that item I'd say in local storage get item and then the key which is name and that's going to bring me back Shawn hopefully there we go now what if we wanted to save some kind of object because our books are objects right we can't save objects directly to local storage the value has to be some kind of string but what we could do is just Jake stringify our object and that's going to turn it into a Jason string version of the object save that and then after we retrieve it later using gets item then we can pass that back into a JavaScript object so for example say we have a book so const book is equal to some kind of object I will just say title is equal to blah and then the author is equal to blah sounds like a very interesting book okay so that is our book so if we take book now we see the subject now what if we wanted to save this to local storage well let me just clear first of all we still have access to oops that didn't work let me just click this we still have access to book even though I've cleared just wanted to give myself some more room now if I want to save this to local storage first of all I'm going to have to jason stringify so I could say local storage dots set item and then I'm going to set an item called my book and then I'm going to jason dot stringify the book so it's going to take this object and turn it into a JSON string and then we're going to save that to local storage so if I press ENTER and then go to application and local storage we can see my book and we get this this is a string right here it's adjacent string so now if I go to get the item let me say local storage get item and we want my book and in fact I'm going to store this in a constant so I'll say result is equal to this press ENTER and then look at the results now we can see that this is a JSON string of an object so what we need to do is pass that back into a JavaScript object in order to use it properly inside our application so all we need to do is say jason dots pass and pass the result in and then that gets us back the object so that's how we can kind of overcome that hurdle of not being able to store objects directly which stringify and then we pass the results so now we know the basics of local storage let's apply this to our application so the first thing I'll do is just clear this and then I want to go to application and I want to clear out these different items so we've got a blank local storage so that's absolutely fine and now we can go ahead and implement it inside this book context ok so the first thing we want to do is save the data when a user type something into these fields and presses that book I want to take that book and I want to save it to local storage when that happens now at what point can we do this inside our context well if you remember we have this hook called use effect and that hook is going to run every time the data updates so currently if we add a new book in the form then this book's data is going to update right so this use effect hook would run so at that point we could take that data that we currently have for books and we could store it in a books property inside a local storage that seems like a good idea to me so let's now say use effect and inside we need a callback function which is going to run whenever the data changes right here and as a second parameter we'll do an array and say books inside it so that means that whenever the books data changes then run this hook so then inside we want to say local storage and we want to set an item the item name or the key is going to be called books so it's just going to mirror it up to these books right here and we need to Jason string if I the array so we'll pass in the books right there which is the data we have so we string key find that turning it into a JSON string and we're setting that item every time we add something to the list so now if I save this and come over here you can see we have this box array right here and that's wrong because remember first of all this runs on first render so because box is empty at the minute and that's what we've saved you localstorage just an empty array but the minute we add some kind of book title are saved the way of Kings by Brandon Sanderson you've probably gets to one of my favorite authors is by the end of this series now you can see that we have this thing right here so now we have an array with one book inside it now if we add another one I'll say the name of the wind and that is by Patrick Rothfuss if I press ENTER then it's going to update over here so now the books property has a value which has two elements inside of it and that's because when we updated the books array and this changed we're resetting this box item we're overwriting it with the new data okay so every time we add a new book it's updating what we're storing inside local storage so that's nice but still when we refresh we still don't see anything here and this has got back to normal so that's not a good idea it goes back to normal because on first render we are basically just setting this to be an empty array because that's what it begins us so what we need to do is let this begin as something else and there's something else we want to let it begin as is what's already in local storage at that point when we first refresh the application so we can actually pass a third argument to use reducer and that is a function and that function should return a value which it will take as the default value so it will ignore this one and take whatever we returned inside the function instead so let's do that function and error function and inside this what I'll do is I'll try to get the books item from local storage so I'll say Const local data is equal to local storage get item and we want the books now we want to only return this item if there's something in here so what we'll say is local data question mark but ternary operator if that's true then we want a Jason dots Pass whatever is in local data so we're getting that data and passing it into a JavaScript object and then we actually want to return this value right here so if this is true and we have something we're going to get that data and pass it and return it and since we're returning it it's going to take that value as the default initial value of books and then otherwise we want to just pass an empty array so if this is false we don't have data then we're just going to use an empty array instead so now if I save this this should work to begin with we don't have anything because there's nothing in there but if I now say blah or whatever and then add this book and then if i refresh now now we get that book because we're setting it as the initial value or right here so let me just add another one the way of Kings by Brandon Sanderson and at that book and if we refresh we can still see that here delete one then refresh and this now is being kept in sync with what we have in local storage so then gang that is it we've completed this application and also completed the whole series so I really hope this has been of some use to you if you do enjoy the videos my friends please do not forget to share subscribe and like that really means a lot and I'm gonna see you in the very next tutorial series you

Original Description

Hey gang, in this final React Hooks & Context tutorial, we'll add local storage into the mix so that we can actually save books on reload. ---------------------------------------- 🐱‍💻 🐱‍💻 Course Links: + Course files - https://github.com/iamshaunjp/react-context-hooks + Complete React Course - https://www.youtube.com/playlist?list=PL4cUxeGkcC9ij8CfkAY2RAGb-tmkNwQHG + React Context Docs - https://reactjs.org/docs/context.html + Modern JavaScript Tutorial - https://www.udemy.com/modern-javascript-from-novice-to-ninja/?couponCode=NINJAYT 🐱‍💻 🐱‍💻 Other Related Courses: + Firebase Firestore Playlist - https://www.youtube.com/watch?v=4d-gIPGzmK4&list=PL4cUxeGkcC9itfjle0ji1xOZ2cjRGY_WB Firebase Authentication Tutorial - https://www.youtube.com/watch?v=aN1LnNq4z54&list=PL4cUxeGkcC9jUPIes_B8vRjn1_GaplOPQ
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related AI Lessons

Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Prepare for a frontend developer interview with Capgemini by reviewing JavaScript fundamentals and practicing common interview questions
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with 10 essential tools for modern web app development
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with top 10 developer tools in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
US frontend engineer hiring demand peaked in 2022 and remains flat-depressed in 2026, contrary to common assumptions
Dev.to AI
Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →