1

I could render the herader's table users through for exemple: {user[name]}. But I couldn't render the header itself who is the [name] for example rendering: name of Leanne Graham. Can anyone help me with this?

 import React from 'react'

        const Users= ({ users}) => {
           return (
                 <div>
                    {users.map((user,index) => (
                       <div key={index}> 
                          <div className="container smcontainer d-flex justify-content-start"> 
                             <div className="row">
                             <div className="col-md-12">
                                <table className="table table-striped">
                                         <thead>

                                         </thead>
                                      <tbody>
                                         <tr>
                                            <td className=""> {user['id']} </td>  
                                            <td className=""> {user['name']} </td>  
                                            <td className=""> {user['username']} </td>  
                                            <td className=""> {user['email']} </td>   
                                         </tr>
                                      </tbody>
                                </table> 
                             </div> 
                          </div> 
                       </div>
                       </div>
                    ))}   
                 </div>
           )}
        export default Products

This is the App component

 class App extends Component {
      constructor(props){
        super(props)
          this.state= {
            users: [], 
        } 
      }
        async componentDidMount() {
              const url = ('https://jsonplaceholder.typicode.com/users')
              const response = await fetch (url)
              const data = await response.json() 
              this.setState({users: data.itemsList})
              console.log({users: data.itemsList})        
                 }

          render() {
            return (
              <Users users = {this.state.users} />
            )
          }
      }
    export default App;
3
  • Sorry, don't understand what you're trying to do. Commented Oct 6, 2019 at 0:58
  • I'm tying to render the keys of table (the header). I succeded to render the value of keys. But couldn't render their values, in <th> section above. Commented Oct 7, 2019 at 21:57
  • Are you trying to make a table of users or a separate table for each individual user? Commented Oct 7, 2019 at 23:33

2 Answers 2

1

When you map over an array, whatever you return is returned each time for every element in the array. This code will create a whole new table for each user.

I think what you want to do is define your column headers separately from your map call and then map over the array to generate the rows:

const columns = ["ID", "Name", "Username", "Email"];

...

<div>
        <div className="container smcontainer d-flex justify-content-start"> 
           <div className="row">
            <div className="col-md-12">
               <table className="table table-striped">
                 <thead>
                    {columns.map(c => <th>{c}</th>)}
                 </thead>
                 <tbody>
                    {users.map((user, index) => (
                       <tr>
                         <td className=""> {user['id']} </td>  
                         <td className=""> {user['name']} </td>  
                         <td className=""> {user['username']} </td>  
                         <td className=""> {user['email']} </td>   
                       </tr>
                   ))}
                 </tbody>                 
                </table> 
            </div> 
          </div>
        </div>
      </div>

I made a Codepen demonstrating both ways, but to me the first one makes much more sense.

Sign up to request clarification or add additional context in comments.

Comments

0

That should give you what you want.

<thead> {user['name']} <thead/>

If you want to take the first name of Leanne Graham, you can do:

<thead> {user['name'].split(" ")[0]} <thead/>

1 Comment

I wanna to get the name it self as a header (the keys). Render for example Id, Name, Username, and email.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.