React Context & Hooks Tutorial #18 - Reading List App (part 3)

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

Key Takeaways

Builds a reading list app using React Context and Hooks, specifically the useReducer hook

Full Transcript

okay then so we're making some progress here we've done three of the four components we need to create the last one we need to do is this book form right here and that is going to be a form that sits right at the bottom with two input fields inside it one for the book title and one for the book author so a user can enter those in submit the form and then that book is going to get added to the list so let's get cracking with this component first of all we need to import react and we also need to import a couple of other things we need to import use context because we will be using the book context and also use States because we want a piece of a local state inside this component as well or rather two pieces one for each of the input fields to track what a user types into them so down here let's create this component and we'll call this new book form and inside here the first thing I'm going to do is use context and the context that we want is the book context and then down here we can just say import book context and that's going to be from dot forward slash to come up a level then into the contexts folder then book context like so okay so we have this now and we want to D structure a single property from this and that is going to be the add book function right here so let's now say Const add book is going to be equal to this use context book context okay so we have that add book function available to us now in here now I also want to set up two pieces of state one for the title and one for the author so let me now say Const and it's gonna be title and set to title because remember you state returns as those two things inside an array the actual data and also a function to change the data and we set that equal to use state set an initial value for this which is going to be an empty string and I'm going to do exactly the same thing for author only it's going to be author and set author right here okay so now we can go about actually creating this template now it's gonna be a form so let's create that tag and inside we need an input field for the name of the book it's going to be of type text and then I'm also going to put a placeholder on this as well and that's going to be book title then after that I'm going to say the value is equal to this thing right here title and then I'm going to go to a new line so we've got some more room I'm going to say unchanged is equal to some kind of function which takes in the event object and in here what I'm going to do is update the state of title so to do that we say set title and we want to pass in the value that the user types in to this input field and we can get that by saying e target dot value like so and that's all we pretty much need to do right here so let me now close this off like so and in fact we'll just add on a required attribute as well because this will be a required field so we've done the first one now let's copy this because I also want to do this for the second input field which is going to be the author so we'll just say author right here for the placeholder the value is going to be the author variable right here and also we want to update set koala third not set title so let's update that there and that is everything so we also need one more thing at the bottom and that is just a submit button so I'll say input type is equal to submit and I'll say value is equal to add book like so and that's all we need so then if I was to nest this now in the app KS we should see it let me do that underneath book list I'm going to say book I know it's new book new book form there we go and it also imports it for me so I'm going to nest up there save it and preview and we can see this over here it doesn't look great at the minute but if we type something in at the minute and AD book then it's not gonna do anything special it just refreshes the page which is the default browser behavior when we submit a form what we need to do is attach an event listener to this so that when it's submit we do our own thing so let's say on submit that's equal to some kind of function which we'll call handle submit and we need to define that up here so we'll say Const handle submit and set that equal to an error function we take in the event object because we need to prevent that default behavior so I prevent default to begin with and then what I'd like to do is just log to the console the title and the author to see if this is working and we're tracking these input fuels so save that and blah-dee-blah ID book yet we see those right there so this is working is capturing that data so now let's get rid of that and what we'd like to do is use this function at Bucknell so ad book and remember if we go to the context it takes two parameters the title and the author so let's pass those in so we have the title and the author here storing those variables title and author and once we've added the book and then also like to reset these values so I'm going to say set title and that's going to be an empty string again and then set author and that will also be an empty string again okay so now this should all work let me try this out just enter in any old rubbish at the book and there we go it works try it again and it works and we can still click these off so that's all working them the last thing I'd like to do is just make this look a bit better so again I've got some CSS that I'm just going to copy and paste from my repo let me come into index dot CSS and we can see we have the form which has a padding of 20 pixels the input fields with a type of text they have a width of 100% padding of 10 pixels box sizing of border box so it takes into consideration the padding margin of 6 pixels top and bottom we give it a background color of this kind of deep full text color of white and then a bullet was zero then the submit button we just give a bit of margin top and bottom the background is a light gray no border a bit of padding and then display as block some very simple styles save that and now it looks a little bit better so if I type in here the final Empire and Brandon Sanderson awesome book at the book there we go all works summer friends now we have kind of completed this ninja reading list but there are two more things I'd like to talk about in this series the first one is going to be all about reducers and we're going to implement that into this application as well shortly and the second one is local storage I want to hook this up with local storage so that when we refresh we don't just get the dummy data we'd get whatever is stored in local storage in the browser so we'll start with reduces and we'll talk about what they are in the next video

Original Description

Hey gang, in this React hooks & Context tutorial we'll put together the last parts of our project. Next, we'll look at the useReducer hook. ---------------------------------------- 🐱‍💻 🐱‍💻 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

Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →