关于react Fragments,React 中一个常见模式是为一个组件返回多个元素。Fragments 可以让你聚合一个子元素列表,并且不在DOM中增加额外节点。
render() {
return (
<>
<ChildA />
<ChildB />
<ChildC />
</>
);
}
但是我在实际运用中,碰到了问题,比如一组单选按钮
<RadioGroup defaultValue={this.state.flag} size="large">
<RadioButton value="1" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(1)+' fs16'} />
</RadioButton>
<RadioButton value="2" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(2)+' fs16'} />
</RadioButton>
<RadioButton value="3" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(3)+' fs16'}/>
</RadioButton>
<RadioButton value="4" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(4)+' fs16'}/>
</RadioButton>
<RadioButton value="5" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(5)+' fs16'}/>
</RadioButton>
</RadioGroup>
所有的单选按钮是有规律可选,为了优化代码,我打算进行简写,提到一个方法里面,提到方法后,不能用容器去包裹这些RadioButton,包裹后,这个组件的一些功能会失效,比如默认选中,所以只能用空标签,想到了之前看到的flagments
https://doc.react-china.org/docs/fragments.html
eg:
radiosBtns(){
return(
<>
<RadioButton value="1" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(1)+' fs16'} />
</RadioButton>
<RadioButton value="2" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(2)+' fs16'} />
</RadioButton>
<RadioButton value="3" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(3)+' fs16'}/>
</RadioButton>
<RadioButton value="4" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(4)+' fs16'}/>
</RadioButton>
<RadioButton value="5" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(5)+' fs16'}/>
</RadioButton>
</>
)
}
我发现报错了

然后查了些资料,找了些方法
 
 
 
第一种,直接返回数据
react 16开始, render支持返回数组,知道这个特性的人不在少数。这一特性已经可以减少不必要节点嵌套,小伙伴们可以多多用起来。
radiosBtns(){
return(
[
<RadioButton value="1" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(1)+' fs16'} />
</RadioButton>,
<RadioButton value="2" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(2)+' fs16'} />
</RadioButton>,
<RadioButton value="3" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(3)+' fs16'}/>
</RadioButton>,
<RadioButton value="4" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(4)+' fs16'}/>
</RadioButton>,
<RadioButton value="5" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(5)+' fs16'}/>
</RadioButton>
]
)
}
以为这样就好了,但是悲剧的是,还是报错了,

Each child in an array or iterator should have a unique "key" prop.数组或迭代器中的每个子元素都应该有一个唯一的“key”,然后我加了key

https://doc.react-china.org/docs/lists-and-keys.html
当元素没有确定的id时,你可以使用他的序列号索引index作为key
如果列表可以重新排序,我们不建议使用索引来进行排序,因为这会导致渲染变得很慢
https://doc.react-china.org/docs/reconciliation.html#%E9%80%92%E5%BD%92%E5%AD%90%E8%8A%82%E7%82%B9
radiosBtns(){
return(
[
<RadioButton value="1" key="1" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(1)+' fs16'} />
</RadioButton>,
<RadioButton value="2" key="2" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(2)+' fs16'} />
</RadioButton>,
<RadioButton value="3" key="3" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(3)+' fs16'}/>
</RadioButton>,
<RadioButton value="4" key="4" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(4)+' fs16'}/>
</RadioButton>,
<RadioButton value="5" key="5" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(5)+' fs16'}/>
</RadioButton>
]
)
}
这样就可以了
第二种,React.Fragment
Flagments的简写形式是<></>,很吊的样子,但目前有些前端工具支持的还不太好,用 create-react-app 创建的项目就不能通过编译,所以这点懒还是不能偷,多写几个字符
radiosBtns(){
return(
<React.Fragment>
<RadioButton value="1" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(1)+' fs16'} />
</RadioButton>
<RadioButton value="2" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(2)+' fs16'} />
</RadioButton>
<RadioButton value="3" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(3)+' fs16'}/>
</RadioButton>
<RadioButton value="4" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(4)+' fs16'}/>
</RadioButton>
<RadioButton value="5" key="5" style={{marginLeft:'10px'}}>
<Icon type="flag" className={flag(5)+' fs16'}/>
</RadioButton>
</React.Fragment>
)
}
这样就可以了

react里面Fragments的使用的更多相关文章

  1. React 顶层 API

    概览 组件 使用 React 组件可以将 UI 拆分为独立且复用的代码片段,每部分都可独立维护.你可以通过子类 React.Component 或 React.PureComponent 来定义 Re ...

  2. React && ReactDOM

    如果你是 React 的初学者,一定会对 React 和 ReactDOM 感到迷惑.为什么要分成两个包呢?害得我还要引入两次,说好的减轻开发人员负担呢,这丫的在搞什么飞机.带着这个疑问,我们一起来康 ...

  3. Vue躬行记(2)——指令

    Vue不仅内置了各类指令,包括条件渲染.事件处理等,还能注册自定义指令. 一.条件渲染 条件渲染的指令包括v-if.v-else.v-else-if和v-show. 1)v-if 该指令的功能和条件语 ...

  4. 学习React系列(七)——Fragments、Portals、Error Boundaries与WEB组件

    React.Fragment portals Error Boundaries WEB组件 React.Fragment 想象一个场景,想把td包装为组件添加到table中去,代码如下: class ...

  5. [React] Use React Fragments to make your DOM tree cleaner

    In this lesson, we will look at Fragments and how they are useful in achieving a cleaner DOM structu ...

  6. React 在服务端渲染的实现

    原文地址:Server-Side React Rendering 原文作者:Roger Jin 译者:牧云云 React 在服务端渲染的实现 React是最受欢迎的客户端 JavaScript 框架, ...

  7. [译文]React v16(新特性)

    [译文]React v16(新特性) 查看原文内容 我们很高兴的宣布React v16.0发布了! 这个版本有很多长期被使用者期待的功能,包括: fragments (返回片段类型) error bo ...

  8. React版本更新及升级须知(持续更新)

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; text-align: center; font: 18.0px "PingFang SC Semibold& ...

  9. React简明学习

    前面的话 React让组件化成为了前端开发的基本思路,比传统思路可以更好的控制前端复杂度,旧的开发方法受到了影响,如分离式的HTML/CSS.非侵入式JS.模板语言.MVC.CSS文件.Bootstr ...

随机推荐

  1. itchat教程

    https://www.python.org/ftp/python/3.6.6/ https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tar.xz ...

  2. source vs export AND ctrl d vs ctrl z

    在脚本中export,只在当前shell脚本进程和子进程中有效 source的作用中是将export的变量在当前脚本环境生效, 如果是在父脚本中执行source,在子脚本中执行export, 父脚本退 ...

  3. 26-----BBS论坛

    BBS论坛(二十六) 26.发布帖子前台代码逻辑完成 (1)front/hooks.py from .views import bp from flask import session,g from ...

  4. java——ArrayList 的存在有什么意义?

    好像所有的数据类型都可以用比如 TreeMap[]  int[] Object[] 这种形式来创建自己的数组,那么ArrayList存在的意义是什么呢? 我只能想到这种:ArrayList可以存储多种 ...

  5. Python Pandas -- DataFrame

    pandas.DataFrame class pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False) ...

  6. 转 Python3 错误和异常/ Python学习之错误调试和测试

    ########sample 0 https://www.cnblogs.com/Simon-xm/p/4073028.html except: #捕获所有异常 except: <异常名> ...

  7. 利用Intent.ACTION_SEND进行分享

    安卓系统本身可以很简便的实现分享功能,因为我们只需向startActivity传递一个ACTION_SEND的Intent,系统就为我们弹出一个应用程序列表.其实在系统的文件管理器中,这应该是我们常用 ...

  8. 怎么为android控件边缘添加阴影

    为控件设置一个有阴影感的背景图片即可,可以使用shape 在自定义shape中增加一层或多层,并错开,即可显示阴影效果.为增加立体感,按钮按下的时候,只设置一层.我们可以通过top, bottom, ...

  9. 【PHP】 hash加密

    之前对密码进行加密是在JS文件里做的,但是不行,改到PHP文件里进行加密比较安全,于是乎…… [原JS加密代码] var password = sha256_digest(pwd); 后来在网上调查方 ...

  10. [转]png图片压缩大小但是不改变透明部分

    降低PNG图片存储大小方法,图片压缩方法,如何降低PNG图片存储大小?前提是分辨率和尺寸大小不变,图形的透明部分不变.请看如下办法,亲测可用. 1. 将PNG图片用PS打开. 2. 图像-模式-8位/ ...