React GET HTTP call using fetch library | Functional Component
GET CALL
fetch method returns a promise, which we access basically using then method.
response.json() : converts the data into JSON
import React from "react";
const FetchApiGet = () => {
const [data, setData] = React.useState({});
const fetchApiData = () => {
fetch("https://jsonplaceholder.typicode.com/todos/1")
.then((response) => response.json())
.then((json) => setData(json));
};
return (
<div>
<pre>{JSON.stringify(data, null, 2)}</pre>
<button className="btn btn-primary" onClick={fetchApiData}>
Fetch Data
</button>
</div>
);
};
export default FetchApiGet;
Comments
Post a Comment