0

I am trying to build a dropdown-menu and add data from my array to the dropdown-item. My current code isn't returning anything into my const Users. How can I use the array to add data into the dropdown-item?

UserDisplay component

   const UserDisplay = ({ users }) => {
      const Users = users.map(user => {
        let i = 0;
        <a className="dropdown-item" href="#">
          {user[i]}
        </a>;
        i++;
      });
      return (
        <div className="dropdown-menu" id="users">
          <a className="dropdown-item" href="#">
            Online Users
          </a>
          <div className="dropdown-divider" />
             {Users}
        </div>
      );
    };

Parent Component ChatLayout

return (
      <div className="chat navbar fixed-bottom">
        <div className="btn-group dropup">
          <button
            type="button"
            className="btn btn-secondary dropdown-toggle"
            data-toggle="dropdown"
            aria-haspopup="true"
            aria-expanded="false"
          >
            Chat
          </button>
          <UserDisplay users={[this.state.users]} />
        </div>
        <ul id="messages">
          <div />
        </ul>
        <form onSubmit={this.onSubmit}>
          <textarea
            name="message"
            placeholder="Enter your message here"
            autoComplete="off"
            type="submit"
            onKeyDown={this.onEnterPress}
            value={this.state.message}
            onChange={this.onChange}
          />
          <input type="submit" className="btn btn-info btn-block mt-4" />
        </form>
      </div>
    );
5
  • Can you add all of the relevant code? Does UserDisplay live inside a component? Where is the parent component? Commented Aug 11, 2018 at 14:54
  • @Themes.guide, done. I added the parent component return Commented Aug 11, 2018 at 15:02
  • Why do you want to use an index for user[i]? You can use the index passed to the function instead users.map((user, i) => { ... }). You are not returning the JSX from the function given to map either. Commented Aug 11, 2018 at 15:04
  • I didn't know I could use that index. And as far as not returning the function given to map, why wouldn't using the return statement with the const Users work? Since im not retrurning from map, I am using that value im creating. @Tholle Commented Aug 11, 2018 at 15:15
  • The function given to map (users.map(function myFunction() { ... }) has to return something, or else the resulting array will just contain a bunch of undefined values. You need to return <a className="dropdown-item" href="#"> .... Do you really want to access user[index]? There might be something else in the user object you want to render. Commented Aug 11, 2018 at 15:18

2 Answers 2

0

You don't need to define and iterate i.. the .map already keeps track of the array index. Assuming users has data it should work like this...

  UserDisplay(users) {
      const Users = users.map((user,i) => {
        return (
            <a className="dropdown-item" key={i} href="#">
              {user}
            </a>)
      });
      return (
        <div className="dropdown-menu" id="users">
          <a className="dropdown-item" href="#">
            Online Users
          </a>
          <div className="dropdown-divider" />
          {Users}
        </div>
      );
  };

Working Codeply: https://www.codeply.com/go/DnqpGhozra

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

Comments

0

You are destructing props object and get users out of it so its fine. basically map returns a list of array so you dont have return anything.

You need to iterate to the first element of users like

const Users = users[0].map((user,i) => {
        console.log(user);
        return (
            <a className="dropdown-item" key={i} href="#">
              {user}
            </a>)
      });

OR just pass users directly

 <UserDisplay users={this.state.users} />

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.