Full React Tutorial #26 - Reusing Custom Hooks
Skills:
React90%
Key Takeaways
Reuses a custom hook to fetch blog details with React
Full Transcript
all right then so now we can click on one of these links to go through to the blog details page where we have access to that route parameter the id for that blog so we can use that inside this component now to make a fetch request for the details of that particular blog from this db file and to do that we're going to be reusing our custom hook use fetch now remember this returns three values the data we're trying to fetch in our case the individual blog and is pending property which is true or false and also an error if there is one and all we have to do is use this hook in the blog details component now and pass in the url or the end point that we want to use to fetch data from so that's what we're going to do inside blog details so let's first of all down here say const and we want to grab a few things from this use fetch hook we want the data first of all then we want the error if there is one and then the is pending state as well and we're going to set that equal to use fetch and we need to import that as well at the top so let me say import use fetch from dot forward slash use fetch okay so we just need to pass in the end point right here so i'm going to paste this in so it's local host port 8000 forward slash blogs forward slash and then we need the id of the blog we're trying to get now we have access to that because we use this use params hook to grab it right here so i can just tack it on the end by adding a plus and then id all right so now we're using this hook to get these three properties now inside this component i want to call this data property right here block so let me do that by saying colon and then blog so now down here we can use this blog property all right so at some point we're going to have this data we might have an error and we might have true or false for its pending so i want to conditionally output a bit of template dependent on these values so let's get rid of the h2 and start this first of all i want to output a loading message if this is true so again curly braces and we'll say is pending and then double ampersand let's spell this correctly first double ampersand logical and so the thing on the right only outputs if the thing on the left is true and this is going to be a div and inside that div oops let me just create this tag we're going to do a loading message so we'll just say loading dot dot dot all right and after that i want to output the error if there is one so error double ampersand and then another div and inside this we want the error if there is one close off that div and then finally we want a bit of template for the blog itself once we have blog details once we have a value for the blog now to begin with that starts as null right here the data so we don't want to output it if it's null only when we have a value so we'll say blog double ampersand and then we'll return some kind of template in the parentheses now that template is going to be an article first of all inside the article i'll do an h2 for the blog title so blog.title and then oops again spell this correctly and then below the h2 we'll do a paragraph to say written by and then blog dot author and then finally we want the body property inside a div so let's do that div and then we'll say blog dot body now if we take a look at the data that's this property right here the actual contents of the blog so i think that is all correct let me save this now and preview and we see first it loads then we get all the details right here let's try going from the home page to one of these click on this yep works and let's click on the other one my first blog yep works and also if you just typed in the url directly it would still work okay cool so then i just want to add a bit of css to this to make it look a little bit better so i'm just gonna paste these into the index file over here like so so just three styles first of all the h2 to increase the font size give it a color and a margin bottom then the div which is if we take a look over here this thing the actual body of the contents we give that a margin top and bottom and then finally we have a button but we don't have a button inside here yet so let's get rid of that we only need the first two styles that's coming later when we start to delete the blogs and we have a button for that delete so let's save that and our refresh very subtle not much at all but it just spaces things out a little bit all right so hopefully now you can see how reusable this use fetch hook is because we're just passing in the end point whenever we want to get some data and we're getting these three properties back every time an error if there is one and is pending state and the data itself so we can reuse this over and over again whatever the end point is in different components so really really useful next up we're going to turn our attention to this create page and look at forms
Original Description
In this tutorial we'll re-use the custom useFetch hook that we made earlier, to fetch the details of a single blog.
🐱💻 🐱💻 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: React
View skill →Related Reads
📰
📰
📰
📰
Why Your React App Freezes Even With Zero API Calls (And How Web Workers Fix It)
Dev.to · ARAFAT AMAN ALIM
React 19 Features — What Actually Changed and What I Use
Dev.to · Safdar Ali
The Share Button Is the Product: Engineering a Viral Loop in Vanilla JS
Dev.to · yunjie
React, Explained Directly — Episode 1: The Fundamentals
Dev.to · surajrkhonde
🎓
Tutor Explanation
DeepCamp AI