Full React Tutorial #29 - Making a POST Request
Skills:
JavaScript Fundamentals80%
Key Takeaways
Makes a POST request in a React application using the Fetch API to add data to a JSON server
Full Transcript
all right then so now we can submit the form and we can create these blog objects the next step is to make a post request to json server to add that data to our data file so we're not going to use the use fetch hook this time we could probably edit this so we can handle post requests as well and make a more universal hook but i don't want to do that instead i'm just going to make the post request inside this handle submit function because we're only going to make that request once in the whole application not in two different places okay so first of all i'm going to delete this console log at the bottom we don't need that and instead we're going to make a fetch request using the fetch api and this is going to be to the same end point as the get request to get all of the blogs so let's go to the home page and grab this endpoint right here copy that and paste it in here because i was too lazy to write it out from scratch get rid of the extra quotes and now what we need to do is add in a second argument and this second argument is where we tack on the data but also define the type of request we're sending the method if you like because this is going to be a post request so we say method is post we also need a headers property and that is so we can say the content type that is being sent so let's say content hyphen type like so and that is going to be application forward slash json so this is basically telling the server the type of content that we're sending with this request we're sending json data now the next property is the body which is the actual data we're sending with this request and that data is going to be this blog but we don't just say blog first of all we have to turn this from an object into a json string and to do that we use the json object then we use a method called stringify and we pass in the object we want to turn into a json string and that is the block and that's pretty much it so this will then make a post request to this endpoint which is the end point we need to make a post request to add in a new blog and that will add a new blog for us a json server will automatically add the id property as well now since this is asynchronous and it returns a promise we can tack on a then method which fires a function when this is complete so what i could say down here is console.log and we'll just say new blog added like so so let's give this a whirl first i'm just going to add this blog right here like so and it says new blog added and then if we go to the home page we should be able to see that new blog right here awesome so that's worked but what i'd also like to do is maybe add in a loading state when we add the blog and it says in the button something like loading so let's try that i'm going to create another piece of state up here const and we'll call it is pending and then set is pending like so and i'm going to set that equal to use state and to begin with it's going to be false because when we first load the page we're not making that request straight away it's only after we try to submit the form so that's right here so let's inside here say set is pending and we're going to set it equal to true now at this point down here we want to set it equal to false when it's completed so we can say set is pending to be false right here okay then so now down here what i'd like to do is output one button if is pending is false and that's this one so we can add the blog and then a different one if is pending is true because at that point i want to say on the button something like loading and also maybe make it disabled so we can't click it so i'm going to surround this in curly braces and then i'm going to say not is pending so then double ampersand and this is only going to output if this is false which is what we want and then down here below that i'm going to duplicate this and change it to just is pending so if it's true we want to output a disabled button and instead we'll say adding blog dot dot dot like so save it and now we can see that we see adding blog right here and that's because is pending is it true so is pending it should be not a string just false that's why it was true so save that and now we can see add blog right now if we try to add another blog and click it it should go to adding blog like that and then when it's complete it goes back to ad block so that's a bit better you could add a different loading message if you prefer i'm going to stick with this now it would also be nice if we could redirect the user to maybe the home screen after the blog has been added and we'll see how to do those redirects next
Original Description
🐱💻 🐱💻 Course Files:
+ https://github.com/iamshaunjp/Complete-React-Tutorial
🐱👤🐱👤 JOIN THE GANG -
https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join
🐱💻 🐱💻 My Udemy Courses:
+ Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript
+ Vue JS & Firebase - https://www.thenetninja.co.uk/udemy/vue-and-firebase
+ D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase
🐱💻 🐱💻 Helpful Links:
+ HTML & CSS Course - https://www.youtube.com/watch?v=hu-q2zYwEYs&list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G
+ Modern JavaScript course - https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc
+ Get VS Code - https://code.visualstudio.com/
🐱💻 🐱💻 Social Links:
Facebook - https://www.facebook.com/thenetninjauk
Twitter - https://twitter.com/thenetninjauk
Instagram - https://www.instagram.com/thenetninja/
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
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
33
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
📰
📰
📰
📰
How I made a scroll-scrubbed video portfolio fast (Next.js 15 + GSAP + canvas)
Dev.to · Pratham Sharma
5 Reasons HTML Is About to Change Frontend Development
Medium · Programming
5 Reasons HTML Is About to Change Frontend Development
Medium · JavaScript
copilot browser tools make the frontend reviewable
Dev.to · Paulo Victor Leite Lima Gomes
🎓
Tutor Explanation
DeepCamp AI