Axios
A popular HTTP client for making AJAX requests in the browser and NodeJS. Axios provides a smple and intuitive interface for handling asynchronous data fetching,error handling and many more.
Syntax to Install:
npm install axios
Example: Below is an example of axios.
//App.js
import React, {
useState,
useEffect
} from "react";
import axios from "axios";
const App = () => {
const [data, setData] = useState([]);
useEffect(() => {
// Define a function to fetch data from the API
const fetchData = async () => {
try {
// Make a GET request to the API endpoint
const response = await axios.get(
"https://jsonplaceholder.typicode.com/posts"
);
// Extract the data from the response
setData(response.data);
} catch (error) {
// Handle any errors that occur during the request
console.error("Error fetching data:", error);
}
};
// Call the fetchData function when the component mounts
fetchData();
// Clean up any resources when the component unmounts
return () => {
// Optionally, perform any cleanup tasks here
};
}, []);
return (
<div>
<h1>Geeksforgeek</h1>
<ul>
{data.map((post) => (
<li key={post.id}>{post.title}</li>
))}
</ul>
</div>
);
};
export default App;
//index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App'; // Import the App component from the App.js file
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
Output:
8 Most used NPM packages for React
React, a popular JavaScript library for building UI (user interfaces), offers a vast ecosystem of packages to enhance development efficiency and functionality. React allows developers to utilize individual parts of their application on both the client side and the server side, which ultimately boosts the speed of the development process.
Table of Content
- React-router-dom:
- Axios:
- React-Redux:
- Formik:
- React-boostrap:
- React-icons:
- React-datepicker:
- React-slick:
Contact Us