Advertisement
NubeColectiva

Como leer una API REST con axios en React JS

Mar 26th, 2023 (edited)
1,474
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // npm install axios --save
  2.  
  3. import { useEffect, useState } from 'react';
  4. import './App.css';
  5.  
  6. import axios from "axios";
  7.  
  8. function App() {
  9.   const [user, setUser] = useState([]);
  10.  
  11.   const fetchData = () => {
  12.     return axios.get("http://domain.com/api/v1/users")
  13.           .then((response) => setUser(response.data));
  14.   }
  15.  
  16.   useEffect(() => {
  17.     fetchData();
  18.   },[])
  19.  
  20.   return (
  21.     <main>
  22.       <div className='container'>
  23.       <h1>Users</h1>
  24.       <ul>
  25.         {user && user.length > 0 && user.map((userObj, index) => (
  26.             <li key={userObj._id}>{userObj.nya}</li>
  27.           ))}
  28.       </ul>
  29.       </div>
  30.     </main>
  31.   );
  32. }
  33.  
  34. export default App;
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement