[React] Cleanly Map Over A Stateless Functional Component with a Higher Order Component
In this lesson we'll create a Higher Order Component (HOC) that takes care of the key
property that React looks for when using map
to create elements from a list. This HOC will allow us to wrap any component and it will take care of placing the key
prop on the component for us based on the property name that we specify.
Consider following code:
import React from 'react';
import { render } from 'react-dom'; const todos = [
{ id: 1, name: 'Create the initial state', isComplete: true },
{ id: 2, name: 'Map over data', isComplete: true },
{ id: 3, name: 'Refactor', isComplete: true },
{ id: 4, name: 'Use HOC to remove warning', isComplete: false },
]; const TodoItem = (todo) => (
<li
style={{
textDecoration: todo.isComplete ? 'line-through' : '',
}}
>
{todo.name}
</li>
); const App = (props) => (
<div>
<ul>
{props.todos.map(todo => <TodoItem key={todo.id} {...todo} />)}
</ul>
</div>
); render(<App todos={todos}/>, document.getElementById('root'));
When we trying to render <TodoItem>, we use map function, just because we have to add 'key' attr to each TodoItem. We can improve this by using HoC. A HoC is just a function which takes component as arguement and return a function which render the component:
const withKeyFromProps = (Comp, propName) => (props) => (
<Comp key={props[propName]} {...props} />
);
----
[React] Cleanly Map Over A Stateless Functional Component with a Higher Order Component的更多相关文章
- [React Intl] Use a react-intl Higher Order Component to format messages
In some cases, you might need to pass a string from your intl messages.js file as a prop to a compon ...
- [React] Implement a Higher Order Component with Render Props
When making a reusable component, you'll find that people often like to have the API they're most fa ...
- [React] Creating a Stateless Functional Component
Most of the components that you write will be stateless, meaning that they take in props and return ...
- react 创建组件 (四)Stateless Functional Component
上面我们提到的创建组件的方式,都是用来创建包含状态和用户交互的复杂组件,当组件本身只是用来展示,所有数据都是通过props传入的时候,我们便可以使用Stateless Functional Compo ...
- [React] Compound Component (React.Children.map & React.cloneElement)
Imaging you are building a Tabs component. If looks like: <Tabs> <TabList> <Tab> o ...
- React源码解析之React.Children.map()(五)
一,React.Children是什么? 是为了处理this.props.children(this.props.children表示所有组件的子节点)这个属性提供的工具,是顶层的api之一 二,Re ...
- 【原】React中,map出来的元素添加事件无法使用
在使用react中,经常用到react的map函数,用法和jquery里中的map一样,但是,如果你在每个map出来的元素中添加,你会发觉添加的事件无法关联, 比如,我们很多的评论,我需要在每个评论下 ...
- [React] Higher Order Components (replaces Mixins)
Higher order components will allow you to apply behaviors to multiple React components. So the idea ...
- react.js map遍历的问题
React遍历多个Ant Design中的Upload组件时,随意删除任一个Upload出现了bug,依次点击上传图片后,当点击删除时,倒着删除没有问题,从中间和从开头删问题出现了,出现了类似塌方的效 ...
随机推荐
- Java遍历目录下全部文件并替换指定字符串
应用场景:比方有一个深层次的文件目录结构,如:javaAPI 每一个文件中面都有同样的内容,而我们要统一改动为其它内容.上千个文件假设一个个改动显得太不明智. import java.io.Buffe ...
- HDU 1043 八数码(A*搜索)
在学习八数码A*搜索问题的时候须要知道下面几个点: Hash:利用康托展开进行hash 康托展开主要就是依据一个序列求这个序列是第几大的序列. A*搜索:这里的启示函数就用两点之间的曼哈顿距离进行计算 ...
- nginx学习十一 nginx启动流程
今天用了一天的时间看nginx的启动流程,流程还是非常复杂.基本的函数调用有十几个之多.通过看源代码和上网查资料,弄懂了一些函数.有些函数还在学习中,有些函数还待日后学习,这里记录一下今天所学.加油! ...
- mysql 不能启动的两种错误提示及解决方法
在linux系统中安装mysql服务器详细步骤并解决ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passw ...
- 团队作业-Beta冲刺(2)
这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 这个作业要求在哪里 https://edu.cnblo ...
- Java学习笔记二.1
和其他高级语言类似,Java也具有以下部分 1.关键字:见下表,注意Java严格区分大小写,关键字都是小写 2.标识符:见下图 3.注释.有两种://(单行注释)和/**/(多行注释).还有一种文档注 ...
- JavaScript--模块化 JavaScript module designer
module: 模块就是实现特定功能的一组方法.1.在首页的一个接口js;首先下载好require.js文件引入首页. <script src="require.js" da ...
- call、apply、bind 区别
1.为什么要用 call .apply? 为了 改变方法里面的属性而不去改变原来的方法 function fruits() {} fruits.prototype = { color: "r ...
- CISP/CISA 每日一题
CISA 业务流程控制鉴证中要考虑的特定因素: 1.流程图 2.流程控制 3.在流程中评估业务风险 4.对最佳实践进行标杆管理 5.角色与责任 6.活动与任务 7.数据限制 信息系统审计师的任务是 ...
- 【Codeforces Round #451 (Div. 2) A】Rounding
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟 [代码] /* 1.Shoud it use long long ? 2.Have you ever test several ...