React Hooks Tutorial – 13 – Fetching data with useEffect Part 2
![*](https://i0.wp.com/allprowebdesigns.com/wp-content/uploads/2023/12/1703994052_maxresdefault.jpg?resize=840%2C430&ssl=1)
Video Title: React Hooks Tutorial – 13 – Fetching data with useEffect Part 2
In this video let’s learn how to fetch individual posts by passing in the post ID to the get request if we go back to Jason placeholder calm and scroll down to the routes section you can see we have slash posts which returns all the hundred posts and we also have slash
Posts slash one this returns a single post with post ID is equal to one in the URL if we change it to / 2 we get back a post with ID is equal to 2 so in order to retrieve an individual post by post ID we just have to append slash ID to
The current URL we are using in our effect hook let’s see how to do that the first step we need to create an input element that will accept a post ID from the user so within the same component in the JSX before the list of posts I am going to
Add an input element input type is equal to text and this is going to be a controlled component meaning we need to set the value and listen to the unchanged event to assign the value back to the input element so basically we need a state variable I’m going to
Create a new state variable Const ID comma set ID this is equal to you state and we initialize ID with a value of 1 now on the input element we assign ID to value and on change we are going to call set ID so we listen to the event call
Set ID passing in event dot target dot value next we need to make a couple of changes to the existing code the first one changing the URL end point we need to hit slash code /id so I’m going to change to tactics and I’m going to add slash dollar curly-braces ID
So we append the post ID when we make this get request we get back a single post as opposed to an array of posts so let’s change the state variable to post and set post the initial value is going to be an empty object instead of an empty array
Next in the promise we call set post instead of set posts finally in the GS X I’m going to comment out the list of posts and instead render a single post post dot title if you now go back to the browser you should see the title of the
Post with ID is equal to one now when I change the ID to two we still see the old title change the three still the same and that is because we have specified an empty array as the dependency list our effect though now depends on ID so let’s specify that ID
As a dependency if you now take a look at the browser we see post with ID is equal to one if I remove the ID the API returns all the hundred posts as an array which we will ignore for now let’s go with non empty values only
I have ID is equal to one and we can see the title I change it to ten and you can see that the title also changes change it to eleven and you can see the title change again so we are able to successfully fetch an individual post by
Passing in the ID entered by the user now the way we have written the code right now on every character you type into the input field the effect is triggered which executes another data fetching request what would be better is if we could trigger the request on a button click let’s see how
To do that in the next video
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
Done thanks
What do I do if I want to add the name of the person who made the post next to the post title. The post has a UserID but the name of the user is in "Users", not in the post. How are you supposed to connect those requests?
Do you have to make a request to users based on each UserID from the post so the right name is connected to the title? I don't know how to do this.
Best found at 2022.
why Property 'title' does not exist on type '{}'. TS2339 error occured ?
So when i add id in the dependency list, it still does not render the updated title. Any known issue ?
Fantastic , thank you ! <3
Hello actually I realized that you don't need to switch [] with {} when you try to apply a variable /id using an input, but also the ts is giving error that title isn't exist in {} type or something like this so I don't know the way you typed it made the vsc through warning, what do you think I tried [] and it worked.
I understand this is to fetch only the necessary data to save bandwidth/data (fetch by ID) but can we do this as well based on text (title or body text)? well, in real world, no one actually searches by ID of posts or products. newbie here
super … excellent …….u r KT teaches how to code for production proj..
better than udemy paid courses
thanks for your teaching 🤍🤍🤍🤍
I figured out, by using async functions we dont have to use axios and we dont have to specify 'data.data', instead 'data' is enough, the reason I dont know why. Can anyone explain the difference between async await fetch and axios and which one is better?
How do you setState and Array of object? I kept trying but all it does is to use the default value no matter how hard I tried to setstate it
when u add post to the dependency array why does it run infinitely?
Great tutorial
Thank you very much sir for real contribution
just excellent stuff
Good job man!
can we write .post() method instead of .get()? Thanks
if i want to fetching an individual post but clicking on the title of the post i want to read for example!?
How can i get into the post by clicing on it… like a link…
nice, thank you!
how to stop fetch data when id equals 0?
Thanks
Very nice