top of page
Logo der Online Agentur mdwp

REST API

REST API stands for Representational State Transfer Application Programming Interface. It is a set of rules or standards used for building web services. REST APIs are used in React.js (JavaScript library) to handle HTTP requests and responses to create, read, update, and delete (CRUD) operations efficiently.

In the context of React.js, when we want to connect the frontend (React.js) with the backend (e.g., Node.js or Python Flask), we generally use a REST API. The frontend sends an HTTP request using some method (like GET, POST, PUT, or DELETE), then the backend receives this request, processes it, and finally, the frontend receives the HTTP response.

Here is an example of code that uses REST API to fetch data from a specific URL in React.js:

```jsx
import React, { useState, useEffect } from 'react';
import axios from 'axios';

const MyComponent = () => {
const [data, setData] = useState(null);

useEffect(() => {
const fetchData = async () => {
const result = await axios('https://jsonplaceholder.typicode.com/posts/1');
setData(result.data);
};
fetchData();
}, []);

return (
<div>
{data && (
<div>
<h1>{data.title}</h1>
<p>{data.body}</p>
</div>
)}
</div>
);
};

export default MyComponent;
```
In the above example, we used the useEffect hook to send a GET request to 'https://jsonplaceholder.typicode.com/posts/1'. When the data is received, it's stored in the state using the setData function (of useState hook), and then it's displayed on the user's screen.

bottom of page