在反应中渲染输入数组

2024-02-07

我有一系列电子邮件(作为更大模型的一部分)。这些显示在单独的行中,每个行都有一个删除按钮(地址本身可以直接在输入框中更新)。不幸的是,当使用地图函数渲染输入时,我不知道如何在反应中做到这一点。 (我正在将流星火焰项目转换为流星反应)。

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

这是地图功能

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 方法之所以存在,是因为在流星中我需要等待数据,所以同时我需要一个加载状态。

   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. [email protected] /cdn-cgi/l/email-protection;[email protected] /cdn-cgi/l/email-protection"/>
                    <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>
    );
});

注意我是如何绑定的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.
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在反应中渲染输入数组 的相关文章

随机推荐

  • 如何在 Swift 中为 UIView 子类编写自定义 init?

    说我想要init a UIView子类具有String and an Int 如果我只是子类化 我该如何在 Swift 中做到这一点UIView 如果我只是定制init 函数 但参数是一个 String 和一个 Int 它告诉我 在从初始化
  • pyinstaller错误:找不到scipy(没有名为_ufuncs_cxx的模块)

    我正在使用 pyinstaller 将 python 脚本转换为 Ubuntu 14 04 中的二进制文件 我使用 Canopy Enthought 来管理所有 python 库 该代码使用networkx numpy 和scipy 这是我
  • Java 的去中心化集群库 [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 在android中绘制镜像位图

    我正在尝试学习如何在 android 中制作动画精灵 但不知道如何组织我的位图 我有一个角色向右行走的精灵表 一个角色的五个副本的位图 在一个行走周期中等距 每 45 像素 我计划通过一次绘制精灵表位图的一小部分来绘制每个帧 方法是 Rec
  • 实现将内容插入数据库的秘密电子邮件功能

    所以我见过其他大公司 比如 Facebook 这样做 你可以通过电子邮件发帖 这就是我的情况trying to do 用户注册并生成随机电子邮件 密钥 这一步就完成了 然后根据密钥创建实际的工作电子邮件 用户可以在向密钥发送电子邮件的同时输
  • 按钮不适用于更新面板

    我在更新面板中放置了一个计时器和一个用于显示倒计时时间的标签 我已放置下一个按钮 用于在更新面板之外显示下一个问题 我的问题是按钮单击不适用于更新面板 在不使用更新面板和计时器的情况下 它运行良好 我该如何解决这个问题 我还尝试将整个工具放
  • R:xts 中的错误 - order.by

    我正在尝试 重新 构建标准普尔 500 指数的基本预测模型 数据来自雅虎财经 我在数据集的 排序 方面遇到了一些困难 在构建data model期间出现以下错误 xts new x x index 中的错误 NROW x 必须匹配长度 or
  • 从注册表c#检查Windows版本[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我有个问题 如何在 C 中从注册表中检查 Windows 版本 Windows XP 至 Windows 8 1 Environment
  • 警告:org.springframework.web.servlet.PageNotFound - 不支持请求方法“GET”

    我在启动服务器时遇到此异常 HTTP Status 405 Request method GET not supported 我的控制器是 Controller public class HomeController private sta
  • 函数“didUpdateToLocation”被调用而不进行任何更改

    我这样初始化 locationManager if self locManager self locManager CLLocationManager alloc init self locManager delegate self loc
  • OSGi 捆绑包无法启动 - 无法解析 sun.reflect.generics.reflectObjects

    在对 AEM 项目的代码进行看似无关的更改后 我的包无法解析 检查日志后 我可以看到出现以下错误 22 04 2015 11 00 18 650 ERROR qtp1266495948 35 org apache felix http je
  • docker引擎swarm模式需要服务发现

    我对 docker swarm 感到困惑 据我所知 在 docker 引擎为 swarm 模式提供本机支持之前 运行 swarm 的旧方法是在容器中运行管理器和工作人员 旧的容器化 Swarm 的文档解释了如何使用 consul etcd
  • 如何使 NSMutableAttributedString 响应设置应用程序中的动态类型文本

    let dictTitleColor NSAttributedStringKey foregroundColor UIColor LTColor let titleAttributedString NSMutableAttributedSt
  • 调用变量中指定的对象的 Javascript 成员函数

    我有以下所谓的揭示模块模式我想调用这个函数a内部函数b使用变量 我怎样才能做到这一点 foo function a function b function memberName a Call a using value stored in
  • 在另一台计算机上运行时,运行 SWIG 绑定的 Python+C 程序会出现缺少 DLL 错误

    因此 我编写了一个小型测试程序 使用 SWIG 作为 python 和 C 之间的桥梁 该程序的主要部分是 python 文件 该东西在我自己的电脑上运行良好 但一旦我将其转移到另一台电脑上 它立即抛出 ImportError DLL加载失
  • 访问与局部变量(或参数)同名的成员字段

    考虑以下代码片段 struct S S const int a this gt a a option 1 S a a option 2 int a 选项 1 与选项 2 等效吗 是否存在一种形式优于另一种形式的情况 标准的哪个条款描述了这些
  • Django 国际化性能问题(USE_I18N=False 时为 3-4 秒 vs 300 毫秒)

    因此 我们有一个包含大量信息的页面 并且我们经常使用 trans 但是当我们实际使用它们时 USE I18N True 网站就会停止运行 使用 i18n 与使用 i18n 大约需要 3 5 秒关闭时 300 毫秒 我们做了一些分析 似乎翻译
  • 使用 Varnish 时 Magento Onepagechekout 登录问题

    我在用着 Magento v1 7 0 2 Varnish 缓存 v3 0 3 和 IWD 的一页结账 OPC 模块 v2 0 9 我已将 一页结帐 和 一步结帐 路由添加到应从 Varnish 缓存中排除的路由中 我可以看到OPC页面没有
  • 动态使用第一帧作为 HTML5 视频中的海报?

    我想知道是否有任何直接的方法可以实现这种效果 而不需要后端代码来提取帧 将其保存为 jpg 并将其数据库存储在某个地方 当视频加载时 视频的第一帧只显示为海报的效果会非常有帮助 只有当浏览器可以明显地播放视频时 它才会起作用 这可能与方式有
  • 在反应中渲染输入数组

    我有一系列电子邮件 作为更大模型的一部分 这些显示在单独的行中 每个行都有一个删除按钮 地址本身可以直接在输入框中更新 不幸的是 当使用地图函数渲染输入时 我不知道如何在反应中做到这一点 我正在将流星火焰项目转换为流星反应 一切都会呈现 但