reactjsHow do I make a GET request in ReactJS?
Making a GET request in ReactJS requires the use of the fetch
API. Fetch is an interface for fetching resources, and is available in the global scope. It returns a promise that resolves to the response of the request.
Example code
fetch('https://example.com/data')
.then(response => response.json())
.then(data => console.log(data))
Output example
[
{
name: 'John',
age: 30
},
{
name: 'Alice',
age: 25
}
]
The code above makes a GET request to the URL https://example.com/data
. The response is then converted to JSON and logged to the console.
The code consists of three parts:
fetch('https://example.com/data')
- This is the GET request, and it returns a promise..then(response => response.json())
- This is a callback that is called when the promise is resolved. It takes the response from the request and converts it to JSON..then(data => console.log(data))
- This is another callback that is called when the promise is resolved. It takes the JSON data and logs it to the console.
For more information, see the MDN Documentation on the Fetch API.
More of Reactjs
- How do I convert XML to JSON using ReactJS?
- How do I download ReactJS from reactjs.org?
- How do I zip multiple files using ReactJS?
- How can I use the React useContext hook?
- How can I use zxcvbn in a ReactJS project?
- How do I set the z-index of a ReactJS component?
- How do I create a zip file using ReactJS?
- How do I determine which version of ReactJS I'm using?
- How can I use ReactJS and ZeroMQ together to create a distributed application?
- How do I zoom in and out of an image using ReactJS?
See more codes...