简体   繁体   English

如何为ReactJs下拉菜单动态创建选项?

[英]How do I dynamically create options for a ReactJs dropdown menu?

I'm trying to update an existing React select (dropdown) to contain all of the users currently on a server. 我正在尝试更新现有的React选择(下拉列表)以包含当前服务器上的所有用户。 I have an array of all of the users on the server, I just can't get my dropdown to update when the array does. 我在服务器上有一个所有用户的数组,当数组可用时,我只是无法下拉列表进行更新。

Here is my code: 这是我的代码:

 class UserList extends React.Component { constructor(props) { super(props); this.state = {value: ''}; this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } handleChange(event) { socket.emit('join', {room: this.state.value}); this.setState({value: event.target.value}); alert('People on server: ' + this.state.value); event.preventDefault(); } handleSubmit(event) { //not really using this event.preventDefault(); } render() { return ( <form onChange={this.handleChange}> <label> Pick your favorite La Croix flavor: <select id="userlist" value={this.state.value} onChange={this.handleChange}> {this.props.list.map((name) => <option>{name}</option>)} </select> </label> </form> ); } } class Login extends React.Component { render() { return ( <div className="login"> <NameForm /> <UserList list={users} /> <Chat /> <ChatInput /> </div> ); } } 

I'm very new to React, and I don't understand how to get elements to update via external request (in this case a server). 我对React非常陌生,我不了解如何通过外部请求(在本例中为服务器)获取元素进行更新。 Right now, my javascript stores the list of people on the server in an array called 'users' that is updated by the server. 现在,我的JavaScript将服务器上的人员列表存储在服务器更新的名为“用户”的数组中。 I would like the dropdown to update whenever the array does. 我希望下拉列表在数组执行时进行更新。

EDIT: 编辑:

Here is the code that populates the users array: 这是填充用户数组的代码:

 socket.on('update_list', function(msg) { users.push(msg.data); }); 

The users array is definitely updated after the server calls 'update_list'. 在服务器调用“ update_list”之后,绝对会更新users数组。 I have printed the values in users after that is called, and when a new user logs on, users does update accordingly. 在调用之后,我已经在用户中打印了这些值,并且当新用户登录时,用户会相应地进行更新。

The problem is you can't just add to a normal variable and expect React to magically know to re-render the virtual html, ie users.push(...) . 问题是您不能只添加一个普通变量并期望React神奇地知道重新渲染虚拟html,即users.push(...)

setState is the function you want. setState是您想要的功能。 It will update the render methods of itself and it's child components after you call this function. 调用此函数后,它将更新自身及其子组件的render方法。

Your api call needs to be in a component. 您的api调用需要在组件中。

For example, whenever the users change now it will update the render method because we use setState and it will re-render the new users. 例如,每当用户现在更改时,由于我们使用setState ,它将更新render方法,并且它将重新渲染新用户。

Select doesn't have a value attribute. 选择没有值属性。 You want this on the option and probably want users to be an object. 您希望在选项上使用它,并且可能希望用户成为对象。 I'v updated the code. 我更新了代码。

This is just an example. 这只是一个例子。

class UserList extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: ''};
    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
    socket.emit('join', {room: this.state.value});
    this.setState({value: event.target.value});
    alert('People on server: ' + this.state.value);
    event.preventDefault();
  }

  handleSubmit(event) {
    //not really using this
    event.preventDefault();
  }


  render() {
    return (
      <form onChange={this.handleChange}>
        <label>
          Pick your favorite La Croix flavor:
          <select id="userlist" onChange={this.handleChange}>
            {this.props.users.map(user => <option value={user.value}>{user.name}</option>)}
          </select>
        </label>

      </form>
    );
  }
}

class Parent extends React.Component {
  constructor(props) {
    super(props);

    this.getUsers = this.getUsers.bind(this);

    this.state = {
        users: {}
    }
  }
  componentDidMount() {
    this.getUsers();
  }

    getUsers() {
        socket.on('update_list', function(msg) {
          this.setState({users: msg.data});
        }.bind(this));
    }
  render() {
    return (
        <UserList users={this.state.users}/>
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM