make a fetch request in javascript

To make a fetch request in JavaScript, you need to use the fetch() method. Here is an example:

index.tsx
fetch('https://jsonplaceholder.typicode.com/posts/1')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error(error));
167 chars
5 lines

This code makes a GET request to https://jsonplaceholder.typicode.com/posts/1 and logs the response data to the console. The fetch() method returns a Promise that resolves to the Response object. You can then use the json() method on this object to extract the JSON data from the response.

Note that fetch() is an asynchronous method, so you'll need to use .then() and .catch() methods to handle the response or any errors that may occur.

gistlibby LogSnag