Fetch Data in React JS in modern way

<p>There are several ways to fetch data in a React application. Here are some of the most common:</p> <h2>Fetch API :</h2> <p>The Fetch API is a built-in browser API for fetching resources, including data from a server. It returns a Promise that resolves to the response object. You can use the Fetch API in combination with React&rsquo;s useEffect hook to fetch data and update the component state.</p> <pre> import { useState, useEffect } from &#39;react&#39;; function MyComponent() { const [data, setData] = useState([]); useEffect(() =&gt; { fetch(&#39;/api/data&#39;) .then(response =&gt; response.json()) .then(data =&gt; setData(data)) .catch(error =&gt; console.error(error)); }, []); return ( &lt;div&gt; &lt;h1&gt;Data:&lt;/h1&gt; &lt;ul&gt; {data.map(item =&gt; ( &lt;li key={item.id}&gt;{item.name}&lt;/li&gt; ))} &lt;/ul&gt; &lt;/div&gt; ); }</pre> <h2>Axios:</h2> <p>Axios is a popular library for making HTTP requests from a browser or node.js. It returns a Promise that resolves to the response object. You can use Axios in combination with React&rsquo;s useEffect hook to fetch data and update the component state.</p> <p><a href="https://medium.com/@aizaz2117/fetch-data-in-react-js-in-modern-way-f9fe3c8c27f2">Website</a></p>
Tags: Data JS Fetch