[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 keyprop 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,依次点击上传图片后,当点击删除时,倒着删除没有问题,从中间和从开头删问题出现了,出现了类似塌方的效 ...
随机推荐
- BZOJ 1108 POI2007 天然气管道Gaz
题目大意:给定平面上的n个黑点和n个白点.一个黑点仅仅能和右下方的白点匹配.代价为曼哈顿距离,求最小权值完备匹配 STO OTZ STO OTZ STO OTZ ans=Σ(y黑-y白+x白-x黑) ...
- 【吴节操点评】中国企业SaaS应用深谙未来者寥寥数几 两极分化将加剧
数年前,在国外企业级应用如火如荼的时候.国内却是一片空白.而现在企业SaaS应用市场,包含用友.金蝶.东软在内的三巨头.已经有数十家之多.相比美国3000亿美元的企业应用三巨头来说,中国企业应用前三甲 ...
- Oracle442个应用场景-----------Oracle数据库物理结构
-------------------------Oracle数据库物理结构------------------------------- Oracle数据库物理结构 oracle的数据,实际 ...
- 18.Node.js 事件循环
转自:http://www.runoob.com/nodejs/nodejs-tutorial.html Node.js 是单进程单线程应用程序,但是通过事件和回调支持并发,所以性能非常高. Node ...
- Mahout项目开发环境搭建(Eclipse\MyEclipse + Maven)
继续 http://www.tuicool.com/articles/rmiEz2 http://www.cnblogs.com/jchubby/p/4454888.html
- 1.3 Quick Start中 Step 6: Setting up a multi-broker cluster官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Step 6: Setting up a multi-broker cluster ...
- 1.3 Quick Start中 Step 5: Start a consumer官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Step 5: Start a consumer Step : 消费消息 Kafka ...
- 外部事件触发调用对象方法时this指向问题
问题如下: var obj = { name: 'dang', test:function(){ alert(this.name); } }; obj.test(); //这样是可以的 $('.box ...
- Codefroces 832B Petya and Exam
B. Petya and Exam time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- ORA-10458: standby database requires recovery
搭建DG最后一步打开时报错如下: SQL> alter database open read only; alter database open read only * ERROR at l ...