Run into the Storm

I was listening to Broken Walls at the Alaska State Fair. Jonathan Maracle, a Mohawk Indian, shared how they teach to be like the eagle. In a storm, every other animal goes and hunkers down to wait…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Breaking Down the GET Fetch Request in React

Entries rendered to the page in the Daily Journal app.

To illustrate, I’ll breakdown how I use a GET request to pull data into my Daily Journal App in React from my local json server.

React component with the GET request.
User data structure of the db.json file.

Let’s break this down. In useState I set my initial state to an empty array. Then I name my state variable postData and my updater function setPostData. Now my component is stateful and can pass data to the setPostData updater function to store it in the postData variable.

I found that if I tried to call my GET request without using the Effect Hook, the GET request would be called twice, presumably once after the component rendered and then again when the control flow reached the GET request. This would create a bug publishing a duplicate of all data to the page.

Now that I have a successful response from the server, it’s time to render the data to the page. Because I stored the data in the State variable postData, I can traverse across the data array by calling the .map() method on postData.

Please feel free to leave comments, thoughts and recommendations below. Thanks!

Add a comment

Related posts:

Explore The Most Delicious Tomato Ketchup Sauce

Indian kitchen is full of tastes and flavors. It has its own spices to make every dish delicious but there are some adapted tastes like that tomato ketchup sauce. Every Indian kitchen is incomplete…

Lead in Humility

A leader is someone who guides other people into achieving something. There are good leaders and bad leaders. It takes conscious effort to be the former. As Christians and Kingdom leaders…

BlackLight Welcomes You to the Party

Welcome to the Party by BlackLight (aka Carl Davis) takes you back to a time of body-popping and breakdancing to 1980s house music. If you were around in those days, or even if you weren’t, this is a…