1

I have a userlist which contains name and email id of each user. I want to display it using the .map() method on userlist state variable. I have created displayusers() function to display the users but I am getting failed to compile error.

Code:

import React, { Component } from 'react';

class App extends Component {

  constructor(props){
    super(props);
    this.state = {
          userlist:[
          {'name':'Rohan Singh', 
           'email':'[email protected]'
          },

          {'name':'Mohan Singh', 
           'email':'[email protected]'
          },

          {'name':'Rakesh Roy', 
           'email':'[email protected]'
          },

          {'name':'Sunil Shah', 
           'email':'[email protected]'
          }]
    }
  }

  displayusers(){
        return this.state.userlist.map( user => {
          return(
            <div className="item-card">
               <div className="sub">    
                    <div className="type">Username: {user.name}</div>
                    <div className="members">Email: {user.email}</div>
               </div>
            <div className="del-wrap">
                <img src={require("../../images/cancel.svg")}/>
            </div>
           </div>
            );
        })
  }

  render() {
        return(
          <div className="users-wrap">
                <h1>Users</h1>
                <div className="task-content">
                    <div className="user-wrap">
                        <div className="users">
                             {this.displayusers()}
                        </div>
                    </div>
                </div> 
          </div>
        );
    }
}

export default App;
1
  • the displayUsers function which returns the jsx is missing a closing </div> tag Commented May 2, 2018 at 12:13

2 Answers 2

3

I think you forgot about adding a key attribute to the element and there's missing </div> closing tag in your map function.

See the corrected code:

displayusers(){
    return this.state.userlist.map( user => {
        return(
            <div className="item-card" key={user.name}>
                <div className="sub">    
                    <div className="type">Username: {user.name}</div>
                    <div className="members">Email: {user.email}</div>
                </div>
                <div className="del-wrap">
                    <img src={require("../../images/cancel.svg")}/>
                </div>
            </div>
        );
    });
}
Sign up to request clarification or add additional context in comments.

3 Comments

key however recommended but isn't mandatory, problem seems to be with closing div
@UmairAbid I have edited my question by adding <div>
@stonerock does it fix the error when you add the missing <div>?
0

You need to bind your displayusers function to this. You can do that in the constructor.

Update your code as following:

import React, { Component } from 'react';

class App extends Component {

constructor(props){
 super(props);
 this.state = {
      userlist:[
      {'name':'Rohan Singh', 
       'email':'[email protected]'
      },

      {'name':'Mohan Singh', 
       'email':'[email protected]'
      },

      {'name':'Rakesh Roy', 
       'email':'[email protected]'
      },

      {'name':'Sunil Shah', 
       'email':'[email protected]'
      }]
  };

  this.displayusers = this.displayusers.bind(this); // you need to add this line
 }

 displayusers(){
    return this.state.userlist.map((user, index) => {
      return(
        <div className="item-card" key={index}>
           <div className="sub">    
                <div className="type">Username: {user.name}</div>
                <div className="members">Email: {user.email}</div>
           </div>
        <div className="del-wrap">
            <img src={require("../../images/cancel.svg")}/>
        </div>
        );
    })
}

render() {
    return(
      <div className="users-wrap">
            <h1>Users</h1>
            <div className="task-content">
                <div className="user-wrap">
                    <div className="users">
                         {this.displayusers()}
                    </div>
                </div>
            </div> 
      </div>
    );
  }
}

export default App;

3 Comments

I think it's wrong answer. The binding is not required here.
why does it needs to bind context when the function is called with current context
Yes I think ` bind` is not necessary here.

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.