To connect a React app to a backend API, you can use an HTTP client library such as Axios or Fetch to make API requests from your React components. Here's a basic example of how you can do this:
npm install axios
import axios from 'axios';
axios.get('http://your-api-endpoint.com/api/data')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
axios.post('http://your-api-endpoint.com/api/data', { data: 'example' })
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
axios.defaults.baseURL = 'http://your-api-endpoint.com';
By following these steps, you can easily connect your React app to a backend API and fetch data or interact with your server-side logic.