javascript - 在 react 中渲染输入数组

标签 javascript meteor reactjs

我有一系列电子邮件(作为更大模型的一部分)。这些显示在单独的行中,每个行都有一个删除按钮(地址本身可以直接在输入框中更新)。不幸的是,我不知道如何在使用映射函数呈现输入时使用react。 (我正在将 meteor blaze 项目转换为 meteor react)。

一切都会呈现,但我如何附加到更改事件以便我可以更新我的电子邮件数组? onChange + value 需要以某种方式设置。

这是 map 功能

return this.data.emailGroup.emails.map((email) => {
            return (
                <div key={email} className="input-group">
                    <input type="text" className="form-control" onChange={self.handleEmailListChange} value={email}/>

                    <div className="input-group-btn">
                        <button type="button"
                                className="btn btn-default remove-email"><span
                            className="glyphicon glyphicon-remove"></span></button>
                    </div>
                </div>
            );
        });

初始状态(用数据库中的数据填充:

 getInitialState() {
      return {
          name : '',
          description: '',
          emails : [],
          newEmailAddress : ''
      }
    },

根据要求,这里是渲染方法(它需要一个 getContent 方法。getcontent 方法在那里,因为在 meteor 中我需要等待数据,所以同时我需要一个加载状态。

   getContent() {
        return (

            <div className="box box-success">
                <div className="box-header with-border">
                    <h3 className="box-title">List of emails</h3>
                </div>
                <form role="form" className="form-horizontal">
                    <div className="box-body">
                        <p>Below is a list of email addresses which are added to this email group. If
                            you
                            want
                            to add more
                            you can add them one by one by inputting in the box below or add a list into
                            the
                            same box (email
                            addresses have to be seperated by either a space or ;) then press Add to add
                            to
                            the
                            list. You can edit
                            the addresses directly as well as remove them.</p>
                        <div className="input-group">
                            <input type="text" className="form-control"

                                   value={this.state.newEmailAddress}
                                   onChange={this.handleAddNewEmail}
                                   placeholder="Email addresses seperated by a space or a semicolon ; i.e. test1@test.se;test2@test.se"/>
                    <span className="input-group-btn">
                      <button type="button" onClick={this.handleAddNewEmailButton} className="btn btn-info btn-flat add-email">Add</button>
                    </span>
                        </div>
                        <br/>
                        {this.renderEmail()}
                    </div>
                </form>
            </div>
        )
    },
    render()
    {
    var contentStyle = {
        minHeight : '946px'
    };
        return (
            <div className="content-wrapper" style={contentStyle}>
                <section className="content-header">
                    <h1>
                        {this.data.emailGroup? this.data.emailGroup.name : 'hello'}
                    </h1>
                    <small> Created by: Christian Klinton</small>
                    <br/>
                    <small> Last updated by: Christian Klinton - 2015-11-12 08:10:11</small>
                    <ol className="breadcrumb">
                        <li><a href="/emailGroups"><i className="fa fa-dashboard"></i> Home</a></li>
                        <li><a href="/emailGroups">Email groups</a></li>
                        <li className="active">{this.data.emailGroup? this.data.emailGroup.name : 'loading'}</li>
                    </ol>
                </section>
                <section className="content">
                    <div className="row">
                        <div className="col-md-6">
                            <div className="box box-primary">
                                <div className="box-header with-border">
                                    <h3 className="box-title">Information</h3>

                                </div>
                                <form role="form">
                                    <div className="box-body">
                                        <div className="form-group">
                                            <label htmlFor="inputName">Name</label>
                                            <input type="email" className="form-control" id="name"
                                                   onChange={this.handleNameChange}
                                                   placeholder="Set the name of the email group" autoComplete="off"
                                                  value={this.state.name}/>
                                        </div>

                                        <div className="form-group">
                                            <label>Description</label>
                                    <textarea className="form-control" rows="3" id="description"
                                              placeholder="Enter a description what and how the template is used..."
                                              onChange={this.handleDesriptionChange}
                                              value={this.state.description}
                                    ></textarea>
                                        </div>


                                    </div>
                                </form>
                            </div>
                        </div>
                        <div className="col-md-6">
                            {this.data.emailGroup? this.getContent() : <p>Loading</p> }
                        </div>
                        <div className="form-group">
                            <div className="col-sm-offset-8 col-sm-4">
                                <div className="pull-right">
                                    <button className="btn btn-primary">Delete all</button>
                                    <button className="btn btn-primary save">Save</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </section>
            </div>
        )
    }

最佳答案

React 要求您为渲染数组中的每个元素设置一些唯一的东西,它称为 key,它是一个属性。

如果您不知道要为键分配什么,只需为其分配数组的索引即可:

this.props.doors.map((door, index) => (
    <div key={index} className="door"></div>
));

以下是适用于您的问题的相同解决方案:

return this.data.emailGroup.emails.map((email, index) => {
    return (
        <div key={index} className="input-group">
            <input type="text"
                   className="form-control"
                   onChange={self.handleEmailListChange.bind(this, index)} value={email}/>
        </div>
    );
});

请注意我如何绑定(bind) handleEmailListChange 以接收修改后的电子邮件的索引。如果 handleEmailListChange 接受索引,它可以在状态内更新修改后的电子邮件:

handleEmailListChange: function(index, event) {
    var emails = this.state.emails.slice(); // Make a copy of the emails first.
    emails[index] = event.target.value; // Update it with the modified email.
    this.setState({emails: emails}); // Update the state.
}

关于javascript - 在 react 中渲染输入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34321128/

相关文章:

javascript - 带有自定义投影的传单 maxBounds

javascript - Three.js MultiplyVector3 Deprecated 如何替换

javascript - meteor JS : RegisterHelper to display associated user information

javascript - mongoDB:使用父引用查找模型树结构中丢失的文档

node.js - Meteor 需要 Gulp 还是 Grunt?

javascript - Console.log 不适用于 Reactjs 应用程序

reactjs - axios未定义

JavaScript:从嵌套的 JSON 构建 HTML 表格

javascript - HTML/Javascript 页面中 JSON 结果的分页

javascript - react 异步状态