GraphQL Tutorial #30 - Updating Component State
Key Takeaways
This video tutorial demonstrates how to update component state in a GraphQL application using React, by declaring a state for the application, attaching event listeners to form fields, and logging the state to the console when the form is submitted.
Full Transcript
all right there my friends so now we're in a stage where we've got this forum and we want to kind of hook up the functionality to it so that when a user adds some data into it and selects an author press ease plus or add then it's going to make some kind of mutation on the graph QL server and add that data to the database and update the UI here right with the new book so we're going to do this over a series of 1 or 2 videos because there's a few different steps involved the first step is to declare a state for this application to keep track of a few different things we want to keep track of the value of each one of these input fields right so we're going to do that then what we're going to do is attach an event to this thing right here so that when we submit the form it's going to take that state and do something with it eventually it's going to make a mutation on the graph cue our server right but for now what we'll do is maybe just log of the state to the console so we can see what the user has typed in here or selected so let's go ahead and do that to begin with so right now in the code we have our ad book GS component so the first thing I'm going to do is set up the states of this component so we'll do a constructor right here which takes in the props and let me just line this up a little bit all right so inside here we say super props I'm not going to delve into all of this right now this is kind of convention in class-based components in react so then we can say this dot states is equal to an object right and inside this object we can keep track of the different form fields in our component the state of those form fields if you like so to begin with the name is going to be an empty string right and the genre is going to be an empty string and then the author ID is again going to be an empty string this is the initial state of this component because nothing has been selected or typed in our form but as we do type into our form or as we do select an author this state is going to be updated okay so the next thing we need to do is come down here and update the state of this component when I use a DES type something into the farm field or select something down here so let's do these things first of all right so what we need to do is attach an unchanged kind of listener to this farm field so inside here we're going to do an es six arrow function we'll take the event object and pass it through to the function and we're going to say this dot set state which allows us to change the state or set the states and what we want to do is pass through an object with the different parameters we want to update in the state now we want to update the name parameter in this case because this is for the book name so we're going to set the name equal to e which is the events right here dots target which is this input field the target of the event when this changes dots value so it's going to grab whatever is inside this input field at that point when it changes so if I type a K into this input field what it's going to do is detect that fire this function here and update the state so that the name becomes K right if I update it with ke then the state the name property of the state will become gay e so we need to attach this now to not just this but also down here we'll paste that in and instead of name we now want to update this to the genre and then finally we want to add it to this thing the select over here so that when a user selects something and the Select field changes we're going to update the author ID right so remember when we output the options here the value is the author ID of each option so when we select one right here we fire this function and we get the value of that option okay I hope that makes sense so what updating the state here the next thing we need to do is attach an event listener to this thing right here the form so that when it's submitted we fire some kind of function so to do that we'll say on submit set it equal to this to refer to this component dot submit for we've not created this function yet what we will do in a minute and what also going to bind the context of this at this moment in time because this right here refers to the component itself and we want to bind that context to this function so that when we call this inside this function it's also going to refer to the component itself okay so let's create that function now we'll do it beneath the display of this function so and to submit form and we're going to take through the event object as a parameter we can do that when we're reacting to an event right here okay it automatically takes that event object inside what we're going to do is prevent the default action from occurring now previously inside the browser if we type in a load of garbage here and then click on add then all it does is kind of refresh the page that there my friends is the default behavior of this event so now now we want to stop or prevent that default behavior so what we need to do is call a method on this event right here this is not react code this is just general JavaScript and we're going to say prevents default so that prevents the default behavior of this event and it's no longer just going to refresh the page right it's not going to do that then what I'd like to do is console dot log the state of this component so this thing right here so we're going to say this dot States remember we said bind the context of this to this function so inside this function this refers to the component itself so we can say this dot States right so just quickly again we've attached these unchanged listeners to each input type these two text inputs and the Select now when we have a change for example when I select something or when I type something into these fields we're going to call this function which is setting the state of the component in this case it's set the name to the target value so the value inside this input field whatever text is there in this case it's the genre and in this case it's the author ID right because that's the author we selected so when we update the state here it's updating the state properties the name the genre and author ID so when we're finished and we click on the button to add the author at the end what we're doing is calling this function right here and binding the context of this so when this function fires were preventing the default action on the event then what we're doing is logging this state so whatever values are on the name genre and author ID property at that time we're gonna log those to the console okay so let's save all this let's check it out in a browser and now if I say book name bla genre blah blah as you can tell I'm terribly original and will select an author to be Brandon Sanderson click Add then we get this log to the console first of all it did not refresh the page that's because we prevented that default action and so the state's now is logged to the console we have the author ID right here which is the author ID presumably of Brandon Sanderson the genre blah blah and the name blah so we've successfully garnered that information from the form we know now what book the user wants to add to the data to the list if you like so we'll stop there for this tutorial but in the next tutorial we'll look at taking this data and see how we can make a mutation on the graph to our server with it to add this new book to the database
Original Description
Hey gang, in this GraphQL tutorial we'll see how to keep track of the information a user enters into our form by pushing those values to our component state.
DONATE :) - https://www.paypal.me/thenetninja
----- COURSE LINKS:
+ Course files - https://github.com/iamshaunjp/graphql-playlist
+ Atom editor - https://atom.io/a
+ CMDER - http://cmder.net/
+ Node Download - https://nodejs.org/en/
======== Other Tutorials =========
----- NODE.JS TUTORIALS
https://www.youtube.com/playlist?list=PL4cUxeGkcC9gcy9lrvMJ75z9maRw4byYp
----- MONGODB TUTORIALS
https://www.youtube.com/playlist?list=PL4cUxeGkcC9jpvoYriLI0bY8DOgWZfi6u
----- REACT TUTORIALS
https://www.youtube.com/playlist?list=PL4cUxeGkcC9i0_2FF-WhtRIfIJ1lXlTZR
----- SUBSCRIBE TO CHANNEL - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg?sub_confirmation=1
======== Social Links ==========
Twitter - @TheNetNinja - https://twitter.com/thenetninjauk
Patreon - https://www.patreon.com/thenetninja
Playlist
Uploads from Net Ninja · Net Ninja · 33 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
▶
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: JavaScript Fundamentals
View skill →Related Reads
📰
📰
📰
📰
The Cost of Cleverness: A Backend Engineer’s Guide to Strategic Simplicity
Dev.to · Edgar Nahama Alochi
Designing Better Backend Systems for Data That Cannot Be Lost
Dev.to · Marc Keebler
DNS Record Deletion in Node.js: List Identity Before Delete
Dev.to · QuintonShaw1483
Compile HTTP Error Indexes From Exception Classes; Humans Own Recovery Copy
Dev.to · Avery Lin
🎓
Tutor Explanation
DeepCamp AI