Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

reactjs - What should I do to successfully map data from axios API?

How do I map out the data I've put in my console/state? I've been trying to add a map function where I left the "//m", and it seems like it should be simple enough but I can't seem to do it properly.

 import React, { useState, useEffect } from "react"; 
 import axios from "axios"; 
 import EmployeeDetail from "./EmployeeDetail";

 function App() {
    const [employees, setEmployees] = useState([]);
    const [loading, setLoading] = useState(false);
    useEffect(() => {
                       setLoading(true);
                       axios.get("https://randomuser.me/api/?results=10&nat=us")
                            .then(res => { 
                                            console.log(res.data.results);
                                            setEmployees(...res.data.results);
                                            setLoading(false);
                                         })
                            .catch(err => {
                                            console.log(err);
                                          });
                    }, []); 
        
return ( 
  <div className="App"> 
  <h1>Employee List</h1>  
  //m
  </div>
);
}
export default App;  

I was able to make it using the API the guy in the youtube video I referenced used ("https://restcountries.eu/rest/v2/all") with the following function:

   {countries.map((country) => (
<div key={country.name}> 
{country.name} - {country-capital}
</div> 
))}

I'm just having problems with doing it with my own API.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From your question it seems you are looking for rendering a table of output data from an API call.

When you call setEmployees(), react application will refresh the page using virtual DOM as you are setting a state using react hooks mechanism.

return(){
   <div className="App"> 
      <h1>Employee List</h1>  
         <table>
           <thead>
              // your table headers
           </thead>
           <tbody>
                 {this.employees.map((item, index) => {
                                                       <tr>
                                                          <td>{item.value1}</td>
                                                          <td>{item.value2}</td>
                                                          // as per your requirement
                                                       </tr>  
                                                      })}
           </tbody>
         </table>       
  </div>
}
  

One more thing you can do is, create a function and return JSX from function.

Please visit below link for creating function and returning JSX.

How to loop and render elements in React.js without an array of objects to map?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...