Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,151,362 members, 7,812,026 topics. Date: Monday, 29 April 2024 at 06:37 AM

Fetch Data From An API In React Using Functional Components - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Fetch Data From An API In React Using Functional Components (470 Views)

Fetch Data From An API And Display In Flatlist In React Native / Does Opay Have An Api / How To Retrieve Data From An Api Using Fetch Api In Javascript (2) (3) (4)

(1) (Reply) (Go Down)

Fetch Data From An API In React Using Functional Components by kemi72: 9:41am On Sep 25, 2023
In the context of frontend web development, a functional component is a fundamental building block in many modern JavaScript frameworks and libraries. It is a type of component used in frameworks like React and Vue.js, and it can also refer to a similar concept in other programming paradigms, like functional programming.

In the context of React (which is one of the most popular JavaScript libraries that we are dealing), a functional component is a JavaScript function that returns JSX (JavaScript XML) to define the structure and appearance of a user interface element.

You could choose to use either class or functional components in rendering your JSX. For this particular example, we have already demonstrated the “class” example using React Native. See this article

In this tutorial, we are going to fetch data from an endpoint and display the results on our page using, of course, functional components as well as bootstrap for styling.

We are going to be fetching data from this endpoint https://catfact.ninja/breeds . It’s a GET Request and requires no access token or authorization.

The first thing we want to do is create a new react project and add our code. In our App.js or App.tsx, whichever one applies to you, we’re going to add the following:

import { useState, useEffect } from 'react';

const App = () => {
return(
<div>

</div>
}

export default App
Next, we want to have a function that fetches data from the given endpoint. We will create a function and call it fetchItems().

Let us fetch data from the endpoint in React JS.

const fetchItem = () => {
requestAnimationFrame(() =>
fetch(`https://catfact.ninja/breeds`, {
method: 'GET',
})
.then(response => response.json())
.then(responseJson => {
setData(responseJson.data)
console.warn(responseJson.data);
})
.catch(error => {
{
alert(error)
}
}),
);
}
Next, we need to show this data to see that at least we’re getting a response.
Read more https://codeflarelimited.com/blog/fetch-data-from-an-api-in-react-using-functional-components/

1 Like

Re: Fetch Data From An API In React Using Functional Components by john1101(m): 8:43pm On Sep 27, 2023
wow going hard.
Re: Fetch Data From An API In React Using Functional Components by niel63(m): 10:46pm On Sep 27, 2023
kemi72:
In the context of frontend web development, a functional component is a fundamental building block in many modern JavaScript frameworks and libraries. It is a type of component used in frameworks like React and Vue.js, and it can also refer to a similar concept in other programming paradigms, like functional programming.

In the context of React (which is one of the most popular JavaScript libraries that we are dealing), a functional component is a JavaScript function that returns JSX (JavaScript XML) to define the structure and appearance of a user interface element.

You could choose to use either class or functional components in rendering your JSX. For this particular example, we have already demonstrated the “class” example using React Native. See this article

In this tutorial, we are going to fetch data from an endpoint and display the results on our page using, of course, functional components as well as bootstrap for styling.

We are going to be fetching data from this endpoint https://catfact.ninja/breeds . It’s a GET Request and requires no access token or authorization.

The first thing we want to do is create a new react project and add our code. In our App.js or App.tsx, whichever one applies to you, we’re going to add the following:

import { useState, useEffect } from 'react';

const App = () => {
return(
<div>

</div>
}

export default App
Next, we want to have a function that fetches data from the given endpoint. We will create a function and call it fetchItems().

Let us fetch data from the endpoint in React JS.

const fetchItem = () => {
requestAnimationFrame(() =>
fetch(`https://catfact.ninja/breeds`, {
method: 'GET',
})
.then(response => response.json())
.then(responseJson => {
setData(responseJson.data)
console.warn(responseJson.data);
})
.catch(error => {
{
alert(error)
}
}),
);
}
Next, we need to show this data to see that at least we’re getting a response.
Read more https://codeflarelimited.com/blog/fetch-data-from-an-api-in-react-using-functional-components/

When using the useEffect() hook, always tell people about the cleanup function. It's a very good practise to unsubscribe from events when they finish running.

I no too sabi sha... but I think it's really important.
Re: Fetch Data From An API In React Using Functional Components by Frontend: 11:24pm On Sep 27, 2023
React-query or RTK-query is better when fetching data from API endpoint
Re: Fetch Data From An API In React Using Functional Components by kemi72: 11:06am On Oct 04, 2023
niel63:


When using the useEffect() hook, always tell people about the cleanup function. It's a very good practise to unsubscribe from events when they finish running.

I no too sabi sha... but I think it's really important.
Yeah valid point.
Re: Fetch Data From An API In React Using Functional Components by kemi72: 11:10am On Oct 04, 2023
john1101:
wow going hard.
Yeah. No time.
Re: Fetch Data From An API In React Using Functional Components by LikeAking: 1:38pm On Oct 04, 2023
kemi72:
In the context of frontend web development, a functional component is a fundamental building block in many modern JavaScript frameworks and libraries. It is a type of component used in frameworks like React and Vue.js, and it can also refer to a similar concept in other programming paradigms, like functional programming.

In the context of React (which is one of the most popular JavaScript libraries that we are dealing), a functional component is a JavaScript function that returns JSX (JavaScript XML) to define the structure and appearance of a user interface element.

You could choose to use either class or functional components in rendering your JSX. For this particular example, we have already demonstrated the “class” example using React Native. See this article

In this tutorial, we are going to fetch data from an endpoint and display the results on our page using, of course, functional components as well as bootstrap for styling.

We are going to be fetching data from this endpoint https://catfact.ninja/breeds . It’s a GET Request and requires no access token or authorization.

The first thing we want to do is create a new react project and add our code. In our App.js or App.tsx, whichever one applies to you, we’re going to add the following:

import { useState, useEffect } from 'react';

const App = () => {
return(
<div>

</div>
}

export default App
Next, we want to have a function that fetches data from the given endpoint. We will create a function and call it fetchItems().

Let us fetch data from the endpoint in React JS.

const fetchItem = () => {
requestAnimationFrame(() =>
fetch(`https://catfact.ninja/breeds`, {
method: 'GET',
})
.then(response => response.json())
.then(responseJson => {
setData(responseJson.data)
console.warn(responseJson.data);
})
.catch(error => {
{
alert(error)
}
}),
);
}
Next, we need to show this data to see that at least we’re getting a response.
Read more https://codeflarelimited.com/blog/fetch-data-from-an-api-in-react-using-functional-components/

You try!
Re: Fetch Data From An API In React Using Functional Components by LikeAking: 1:39pm On Oct 04, 2023
Frontend:
React-query or RTK-query is better when fetching data from API endpoint

How?

Do the users even care about what was used to fetch?

Fronted guys worry too much..
Re: Fetch Data From An API In React Using Functional Components by kemi72: 1:09pm On Oct 05, 2023
LikeAking:


How?

Do the users even care about what was used to fetch?

Fronted guys worry too much..



Lol comrade be calming down lol

(1) (Reply)

* / How To Create White Board Animation Videos / Download Online Job Portal Using PHP With Source Code

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 45
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.