Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
3.7k views
Welcome To Ask or Share your Answers For Others

1 Answer

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...