React Context & Hooks Tutorial #1 - Introduction

Net Ninja · Beginner ·🌐 Frontend Engineering ·7y ago
Skills: React90%

Key Takeaways

Introduces React Context and Hooks for building stateful React applications without Redux

Full Transcript

hey gang and welcome to your very first react context and hooks tutorial okay they're my friends so unless you've been living under some kind of rock for the last six months or so you probably will have heard about react context and react hooks they're the two still relatively new kids on the block in the react scene and they can have a big impact on how we write react applications so what are they exactly well first of all the react context API that gives us a clean and easy way to share states between different components without having to pass props down all of the time and react hooks allow us to do a whole bunch of stuff inside functional components that we can normally only do inside class components things like you States or tapping to a lifecycle method kind of and when we use them together context and hooks that is when the magic happens together they're gonna help us to create a really nice way to work with shared data inside our application similar to how Redux behaves but without having to install a third-party library you might have even heard through the grapevine that contact hooks have killed he dogs now it's a bit early to be saying that in my opinion but they are a killer combination and I really think you're gonna enjoy using them so before you start this course I would expect you to know a couple of things first of all obviously I want you to have a pretty good grasp of JavaScript because it's a very JavaScript heavy course and secondly you should have a moderate understanding of react and you know how to use the react CLI creating class or functional components using props and all that kind of just now if you don't know any of these then I've got a link to a full modern JavaScript course on udemy that will leave the link to down below and also I have a completely free react course as well for beginners so I'll leave that link down below as well so in this course first of all we're going to learn the basics of the context API and react hooks separately from one another then we're going to merge the two together to create this simple reading list application that is also going to work with local storage in the browser as well now I know this is not groundbreaking stuff right here and it's not gonna win any awards for the best design or anything like that but it is going to give you really good practice using the context API and react hooks together inside an application so as you can see we have a list of books which we can delete and that deletes them from local storage as well and if we add in a book for example the final Empire by Brandon Sanderson and add that we can see it right here and it goes to local storage as well so that if we refresh we don't lose these books it grabs them from local storage and shows them on the screen so we're going to use react context API and react hooks together to create this now I have also created course files for every single lesson that uses code in this series and you can find them at this repo right here react context hooks so I'll leave the link down below to this now each lesson has a different branch in this repo so if you want to see the code from lesson 9 for example so let the lesson 9 brunch and you're gonna see the code right here ok so you can download the code by going to the green button over here clone or download and download the zip or just copy this and clone the repo on your computer finally I am going to be using Visual Studio code for this course so if you want to follow along exactly the way I'm doing it then make sure you download this as well it's code Visual Studio comp that you get this from and just click on the green button to download it there's also a package for this that I'd like to show you as well so then inside of Visual Studio code I'm just going to show you this package that I've already installed so go to extensions and it's called simple reactive snippets so this is one of many packages you can install for vs code that's going to help with react and if we just scroll down here you can see we can use shortcuts to quickly create different things inside react like class components or functional components so I'll be using this as we go along now the first thing I'd like to do is actually create a simple project using the react CLI so I'm going to open up my terminal at the bottom and let me just clear this for some reason it never shows this path when I first open it but anyway I'm going to use MPX first of all then create - react - up and we're going to create an app called context up and just hit enter okay then so now that's installed we can go ahead and see the into context tap into this directory and then say NPM start to spin up the local dev service so we can view it in a browser so I'm just going to clear the console first of all then I'm going to say CD context app to go into this new project folder that we created then I'm going to say NPM start to start the project and view it in a browser over here so hopefully that should open up in a browser and we should see that in a second there we go so this is the dummy application that react boilerplates for us but we don't want to start off with this we want to start off with something a bit simpler so I'm just gonna edit this a little bit go into the source folder and again I will expect you to understand everything I'm doing here if you don't then I think you should start with the react for beginners course and again that link is going to be down below so anyway first of all I'm gonna get rid of a few files so it's not so overwhelming in here I get rid of the test file we won't be using that and also the logo we don't need that we'll get rid of app dot CSS as well just because I'm going to keep it simple and put all of the CSS eventually inside this file right here so a pas that is the root component we can see and we don't need this app dot CSS or logo imports anymore we still need react at the top and inside we'll just delete most of this right here because we don't want all of that we'll keep the surrounding div with a class of app so we'll save that then I'm just going to create a couple of different components so right click new folder to keep these in and I'm going to call it components like so and then well create two components we'll create one for some kind of navbar and one for some kind of book list so let me now say new file and call this navbar is and we're going to flesh this out first of all so first of all we want to import react so I'm gonna say I am to import react we also want to import components as well because we're going to use a class component inside this and then I'm going to use the snippet CC to create a class component then press tab and it creates this boilerplate for me now I'm going to call this navbar and then we don't need any state at the minute so let's get rid of that and inside the return statement down here we just want to return a simple template for now so let's do it now and inside this now if we'll do an h1 first of all we'll just call this context app because we're going to look at the context first of all and then underneath the h1 we're going to say you well and we'll just throw in a few Li tags as well I'm just gonna duplicate this a couple of times I'm going to imagine these are links at the top of the page like a navbar it's not going to go anywhere I'm just putting them there for visual effect just so we've got some content to work with so about and contact and that's the reason and creating this little dummy application by the way just so we've got some kind of working template or application to work with when we start to learn about the context up so I'm just creating a couple of different components to work with as we go forward so that's pretty much the navbar sorted we export it down here at the bottom so now we'll go to app KS and we're going to nest that inside the app div right here so navbar well like so and if I go down here it's gonna also import it for me at the top that's nice so let me close that off now we're nesting the navbar inside the app so if I save and preview in a browser okay we see that it looks pretty cruddy but it's there so let's do one more component you file and we'll call this book list j/s and again this is going to be a class component so let me import react and also see that gets as the component as well so now let me say CC to do another class component and this one is going to be called book list and again we're not going to use state just yet and inside the template all I'm going to do is a div first of all with a class name equal to book list like so and inside the div all we're gonna do is a ul and inside that some Li tags these are just going to be different books so let me just duplicate this a couple of times and first of all we'll say the way of Kings it's a Brandon Sanderson book really good the name of the wind even better and then finally the final Empire okay cool so we've got three books there now and we've done this component as well so now let me save that and import it into a pas again so underneath enough bear we'll do book list it's gonna also import it for me at the top which is nice and let's save that so if we save this now and go to the browser we can see those as well so both of these components are showing now on the page now there's one more thing I'd like to do and that is just to add in a little bit of CSS just so that this thing over here doesn't look so pretty often time so like I said all the CSS is going to go into this indexed or CSS file or right here so if we go over here first of all I'm going to get rid of this because we won't be using that rule at all then I'm just going to paste in a few styles that have come from my github repo on the lesson 1 branch so you can grab them from there if you want to I quickly talk you through these so first of all the app class which is inside here right we're saying that should have a max width of 400 pixels then a margin 30 pixels top and bottom auto left and right and text-align:center so this is just putting this in the middle of the page with a max width and causing all the text to be in the center at the nav which is inside navbar right here that is going to have a padding of 5 pixels all the way around the UL inside the nav we're stripping out the padding because it gets that automatically from a browser using default styles then the Li tags inside the nav we're going to display as inline block so that go left to right instead of top to bottom and give them a margin left and right of 10 pixels 0 top and bottom the book list which is inside this other component right here we're saying we want the padding of 20 pixels all the way around a margin bottom of 20 pixels as well the UL inside that book list adding stripping up the default styles and list-style-type of non meaning that we won't see these little dots right here and then down here the allies inside that book list we're saying padding 10 pixels all the way around margin 10 pixels top and bottom auto left and right and a border radius of 10 pixels as well so that will come in handy later on when we start to colorize these a bit more so let me save this and preview okay so still not looking great but at least it's semi respectable now so there we go this is kind of like the dummy application to get those up and running with react and now starting in the next tutorial we're going to dive right in to the context API

Original Description

Hey gang, in this React Context & Hooks series we'll take a deep dive into how these two new(ish) React features can be used to build Stateful React applications without the use of Redux. ---------------------------------------- 🐱‍💻 🐱‍💻 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 →