React JS Fetch and Map

1 min read
Cover Image for React JS Fetch and Map

fetch is a built-in JavaScript function used to make HTTP requests to a server — like getting data from an API.

Syntax

fetch('https://jsonplaceholder.typicode.com/posts?_limit=5') // Get 5 dummy posts
      .then(response => response.json())
      .then(data => setPosts(data));

Fetch and Map in React

Map will loops through each post in the posts array and renders a <li> for each one.