GraphQL Tutorial #30 - Updating Component State

Net Ninja · Beginner ·🔧 Backend Engineering ·8y ago

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
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 33 of 60

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
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

This tutorial shows how to update component state in a GraphQL application using React, by declaring a state, attaching event listeners, and logging the state to the console when the form is submitted.

Key Takeaways
  1. Declare a state for the application
  2. Attach event listeners to form fields
  3. Log the state to the console when the form is submitted
  4. Prevent default form submission behavior
  5. Use React's setState method to update the component state
💡 To update component state in a GraphQL application using React, you need to declare a state, attach event listeners to form fields, and log the state to the console when the form is submitted.

Related Reads

📰
The Cost of Cleverness: A Backend Engineer’s Guide to Strategic Simplicity
Learn to balance code cleverness with simplicity and maintainability as a backend engineer
Dev.to · Edgar Nahama Alochi
📰
Designing Better Backend Systems for Data That Cannot Be Lost
Learn to design robust backend systems for critical data that cannot be lost, ensuring data integrity and reliability
Dev.to · Marc Keebler
📰
DNS Record Deletion in Node.js: List Identity Before Delete
Learn how to delete a DNS record in Node.js by first listing the zone and matching the record by name
Dev.to · QuintonShaw1483
📰
Compile HTTP Error Indexes From Exception Classes; Humans Own Recovery Copy
Learn to compile HTTP error indexes from exception classes for better error handling and recovery
Dev.to · Avery Lin
Up next
Unlock CRAZY Performance: Native Code Compilation No JNI! #shorts #quarkusinsights #projectpanama
Quarkusio
Watch →