Hello! Hop aboard for a journey through Axios—a JavaScript library renowned for handling HTTP requests. We will discuss installing Axios, using it in React functional components, comparing it with Fetch API, and setting up reusable Axios instances for increased efficiency.
Axios is an HTTP client that enables fetching or posting data. Beyond what the Fetch API offers, it provides automatic JSON data transformation, streamlined error handling, built-in CSRF (Cross-Site Request Forgery) protection, and allows request cancellation!
It's no surprise that Axios is an essential tool in today's web development environment. Often used in conjunction with promises and async/await, Axios proficiently handles HTTP communications. Consider Axios as a messenger between the client and the server on a news website. It fetches the latest news and displays it on your screen.
While Axios comes pre-installed on learning platforms like CodeSignal IDE, we'll explore how to install it in your standalone projects. Using the Node Package Manager (npm), execute this command:
Bash1npm install axios
Now that Axios is installed, to use it in a React application, you need to import it as follows:
JavaScript1import axios from 'axios';
Rest assured, in the CodeSignal environment, you won't need to install it.
Let's use Axios with promises in React. Promises handle future values, and Axios employs them to manage HTTP communication. Consider this example:
JavaScript1function Posts() { 2 const [posts, setPosts] = useState([]); // Initialize state for storing posts 3 4 useEffect(() => { 5 async function fetchData() { 6 const response = await axios.get('https://api-regional.codesignalcontent.com/posting-application-2/posts/'); 7 setPosts(response.data); // Fetch posts data and save posts in a state 8 } 9 fetchData(); 10 }, []); 11 12 // Render posts data 13 return (<div> 14 {posts.map(post => (<div key={post.id}> 15 <h3>{post.text}</h3> 16 <p>Likes: {post.likesCount}</p> 17 </div> 18 ))} 19 </div>); 20} 21export default Posts;
This block fetches data and saves it in the useState
hook. The fetched data is then rendered in the JSX of the component. Note that with async/await
, you need to use try/catch
blocks for error handling since the return value of await does not have a catch method – we skipped it here for simplicity.
The Fetch API, like Axios, handles HTTP protocols. However, Axios has added advantages, such as automatic JSON data transformation and straightforward error handling. Notice how we do not need to use response.json()
when using Axios. The Fetch API might still be a better fit, especially when working with service workers, streams, or caches.
For larger applications, reusable Axios instances would be advantageous. These instances are set up using default configurations and used across different components. See how we create such an instance:
JavaScript1const instance = axios.create({ 2 baseURL: 'https://api-regional.codesignalcontent.com/posting-application-2' 3});
This instance can then be utilized like the main axios object to make API requests. Let's use it in a React component to fetch some posts:
JavaScript1const instance = axios.create({ 2 baseURL: 'https://api-regional.codesignalcontent.com/posting-application-2' 3}); 4 5function Posts() { 6 const [posts, setPosts] = useState([]); // Initializes posts state 7 useEffect(() => { 8 async function fetchData() { 9 const response = await instance.get('/posts/'); // Uses the axios instance to fetch posts 10 setPosts(response.data); // Save fetched posts data in the state 11 } 12 fetchData(); 13 }, []); 14 15 return (<div> 16 {posts.map(post => ( 17 <div key={post.id}> 18 <h3>{post.text}</h3> 19 <p>Likes: {post.likesCount}</p> 20 </div> 21 ))} 22 </div> 23 ); 24} 25export default Posts;
In this code, we re-use the Axios instance to make a GET request to the '/posts/' endpoint. This pattern proves to be incredibly useful in larger projects to ensure cleaner, more maintainable, and more efficient code.
Great job! During this journey, we've explored Axios, contrasted it with the Fetch API, and dived into creating reusable Axios instances. Now, brace yourself for impending practice exercises to refine these concepts. The more code you write, the better you become! Happy coding!