React Hooks Tutorial – 14 – Fetching data with useEffect Part 3
![*](https://i0.wp.com/allprowebdesigns.com/wp-content/uploads/2024/01/1704211107_maxresdefault.jpg?resize=840%2C430&ssl=1)
Video Title: React Hooks Tutorial – 14 – Fetching data with useEffect Part 3
In this video let’s see how to trigger the effect on a button click typically what we would do is create a button and add a click handler within the click handler we made the get request and that is fine but we want to understand how to
Do the same with the effect hook let’s begin the first thing you want is a state label whose value will change only based on the button click the only value we see changing here is the post ID so let’s create a new state variable Const ID from button click set ID from
Button click the default value of 1 next let’s add a button right after the input element button type is equal to button and the text is fetch host on click of this button we want to set the ID from button click variable to the value present in the input field and that
Value is the ID variable which is updated on change so on click let’s call handle click which is going to be a function Const handle click is equal to an arrow function where we call set ID from button click passing in the current ID value which is the text input field
Value the final step is to let the effect know it is now dependent on the ID from button click value instead of the regular ID from unchanged so ID from button click ID from button click and that is pretty much it let’s head back to the browser and test
This out on page load we see the first post title pane displayed I change value to two we don’t see the title change I click on the button and title changes change the three click on the button and we have the third post title so we are now fetching data on
Button click rather than the on change event so that is how you fetch data with us effect with that we come to the end of our set of examples on the effect hook it might seem a bit difficult to grasp at first so go to the videos a
Couple of times and you will find a much better understanding of the effect hook thank you guys for watching feel free to subscribe I’ll see you guys 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
I'm gonna crush this job interview I have tomorrow. You're the best!
Done thanks
Setting a state variable in the onclick handler of a button and adding that variable as a dependency for useEffect where you make the fetch request and update state
This is very very helpful for me I totally confused in useEfect how to use but now I got understood easily thankyou sir ❤️❤️
I have a doubt
I did everything same except I did not made seperate clickHandler instead I wrote the handle logic in button only, like this
<button onClick={setClick(id)}>Click</button>
But it was throwing error that: "Too many re-renders. React limits the number of renders to prevent an infinite loop."
But when I changed the logic little bit, the error resolved, like this,
<button onClick={() => setClick(id)}>Click</button>
I did not understood this, by just making it callback function why error resolved.
I was made it through the ref:
<input ref={myRef} type='text' placeholder="Post ID"></input>
<button onClick={() => setID(myRef.current.value)}>get post</button>
Thank you so much dear sir
Amazing points in each video.. Thank you very much Vishwas!! May God bless you with lots of good health and energy, you have created such amazing tutorials and helping beginners to grasp the content easily!!
another way:
const handleClick = e => {
const input = e.target.previousSibling.value
setIdFromButton(input)
}
return
<input type='text' />
<button type='button' onClick={handleClick}>
Fetch Post
</button>
great tutorial keep up the work
real star in react js
never find such a best tutor than you….Thank you very much
YOU SIR IN ONE WORD ARE AMAZING!!!
what if i want to access an object within another object, like in your code what if i want to access the value of array within that object,
i mean like setPost(res.data) is the object haing another object like org with further data, is this the only way to access it by passing this argument in the setPost as setPost(res.data.org) ? this works in setPost but if i try to access object within object like console.log(res.data) it works but i doesn't work for object within an object like console.log(res.data.org). Can anyone clear confusion?
How to invoke only on button click and not onload (initializing to 1) ?
Nice tutorial . Thanks a lot .
Thanks sir for giving such great content for free of cost. 🙏🙏
Thanks man this really helped a lottttttt
You should redo this video. There is absolutely NO NEED to use useEffect to fetch data upon a button click. A useCallback with standard handler function would be a lot simpler and easier to think and reason about. I've really enjoyed your videos, but I think what you have shown here is an anti-pattern and should not be followed.
where should i keep loader
best channel!
Terrific explanation. Clear and easy to understand 👏🏻. Hats off to you sir🎩
sir how access data from phpmyadimin using API
POST Request
u made my weekend…
your tutorials are great!
excellent demonstration
this was a really bad example tbh. I get that you are trying to illustrate the functionality of hooks but it would be much better if you took the empty dependency array approach. This is a fix for bad code, not the conventional approach.
useEffect is runnning on page load and after clicking button . Is there any way to make it run ONLY when button pressed.