API Integration width Fetch
API integration with Fetch();
Free API :: jsonplaceholder
Fetch API:
Fetch in layman term means "to get".
API is an acronym for Application Programming Interface.
* Fetch Documentation ::
What exactly is an API ?
Now imagine you have an application or a program in your system which needs some data stored at a remote server. For your program to access that data it needs to let the server know about its requirement. And for this purpose it requires a communication interface through which the program can convey its request to the server and the server can respond in a specific format. This interface or the set of rules which our program uses to communicate with other software or server is called Application Programming Interface.
Fetch API
So, Fetch API provides this interface for your programs to be able to send requests to a URL or a remote server and recieve response based on that request in a hassel free way.
In this blog we are going to make a basic app around Fetch API, where we will fetch random todos/ posts/ images etc from the URL https://jsonplaceholder.typeicode.com
,
import React from 'react';
function App() {
fetch('https://jsonplaceholder.typicode.com/posts') .then((response) => response.json()) .then((json) => console.log(json));
return ( <div>
</div> ); }
export default App;
API Fetch with, function and events :
store data in a useState([]);
JSON.stringify()
A common use of JSON is to exchange data to/from a web server.
When sending data to a web server, the data has to be a string.
Convert a JavaScript object into a string with JSON.stringify()
Ex:
const obj = {name: "John", age: 30, city: "New York"};
const myJSON = JSON.stringify(obj);