[Recompose] Transform Props using Recompose --mapProps
Learn how to use the 'mapProps' higher-order component to modify an existing component’s API (its props). 'mapProps' takes incoming props and changes them however you’d like; for example, filtering the props by a field.
For example, we have a UserList component:
import React from 'react';
const User = ({name, status}) => <div>{name} - status</div>;
const UserList = ({users, status}) =>
<div>
<h3>{status} User</h3>
{ users && users.map((user, i) => <User {...user} key={i} />) }
</div>; export default UserList;
And using it to display three different types of user list:
const users = [
{ name: "Tim", status: 'active' },
{ name: "Bob", status: 'active' },
{ name: "Joe", status: 'pending' },
{ name: "Jim", status: 'inactive' },
];
<section>
<h3>Active users</h3>
<UserList users={users.filter(u => u.status === 'active') }/>
<h3>Inactive users</h3>
<UserList users={users.filter(u => u.status === 'inactive') }/>
<h3>Pending users</h3>
<UserList users={users.filter(u => u.status === 'pending') }/>
</section>
Now let's say we want to hide the implement detail, instead just showing three different components.
<div className="App">
<ActiveUsers users={ users } />
<InactiveUsers users={ users } />
<PendingUsers users={ users } />
</div>;
import React from 'react';
import {mapProps} from 'recompose'; const User = ({name, status}) => <div>{name} - status</div>;
const UserList = ({users, status}) =>
<div>
<h3>{status} User</h3>
{ users && users.map((user, i) => <User {...user} key={i} />) }
</div>; const filterByStatus = (status) => mapProps(
({users}) => ({
status,
users: users.filter(u => u.status === status)
})
); export const ActiveUsers = filterByStatus('active')(UserList);
export const InactiveUsers = filterByStatus('inactive')(UserList);
export const PendingUsers = filterByStatus('pending')(UserList); export default UserList;
[Recompose] Transform Props using Recompose --mapProps的更多相关文章
- [Recompose] Lock Props using Recompose -- withProps
Learn how to use the ‘withProps’ higher order component to pre-fill a prop, unable to be overridden. ...
- [Recompose] Compose Streams of React Props with Recompose’s compose and RxJS
Functions created with mapPropsStream canned be composed together to build up powerful streams. Brin ...
- [Recompose] Stream Props to React Children with RxJS
You can decouple the parent stream Component from the mapped React Component by using props.children ...
- 乘风破浪,遇见Android Jetpack之Compose声明式UI开发工具包,逐渐大一统的原生UI绘制体系
什么是Android Jetpack https://developer.android.com/jetpack Android Jetpack是一个由多个库组成的套件,可帮助开发者遵循最佳做法.减少 ...
- [Recompose] Create Stream Behaviors to Push Props in React Components with mapPropsStream
Rather than using Components to push streams into other Components, mapPropsStream allows you to cre ...
- [Recompose] Compute Expensive Props Lazily using Recompose
Learn how to use the 'withPropsOnChange' higher order component to help ensure that expensive prop c ...
- recompose mapProps
mapProps介绍 mapProps函数接收一个函数参数,这个函数参数会返回一个对象用作为接下来的组件的props.组件接收到的props只能是通过mapProps函数参数返回的对象,其他的prop ...
- [Recompose] Refactor React Render Props to Streaming Props with RxJS and Recompose
This lesson takes the concept of render props and migrates it over to streaming props by keeping the ...
- [Recompose] Make Reusable React Props Streams with Lenses
If you hard-code a stream of props to target a specific prop, it becomes impossible to reuse that st ...
随机推荐
- 互联网+时代IT管理者的转型
最近,大众创业万众创新的热潮真是一浪接着一浪,它实际上是一次政府和企事业的自我改革,利用互联网+的思维与技术对生产模式.流通模式与运营模式进行全新的变革,商业的本质是没有变的,仅仅是穿了个马甲来表演. ...
- 华为PUSH SDK 接入方法
本文参考了华为推送平台官网及其Demo:http://developer.huawei.com/cn/consumer/wiki/index.php?title=%E6%8E%A5%E5%85%A5% ...
- php课程 12-40 抽象类的作用是什么
php课程 12-40 抽象类的作用是什么 一.总结 一句话总结:定标准的 1.继承的关键词有哪两个? extendsparent 2.抽象类的实际意义是什么? 制造符合规范的产品你必须实现了抽象类里 ...
- 9.使用 npm 命令安装模块
转自:http://www.runoob.com/nodejs/nodejs-tutorial.html npm 安装 Node.js 模块语法格式如下: $ npm install <Modu ...
- ORACLE11g R2【单实例 FS→单实例FS】
ORACLE11g R2[单实例 FS→单实例FS] 本演示案例所用环境: primary standby OS Hostname pry std OS Version RHEL6.5 RHEL6 ...
- Qt5官方demo解析集28——Extending QML - Signal Support Example
本系列全部文章能够在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集27--Extendin ...
- 把java程序打包成.exe
准备工作:将可执行的jar包跟资源跟第三方包都放到一个目录下. 能够将jre包也放入里面.这样在没有安装jre的情况下也能够执行. watermark/2/text/aHR0cDovL2Jsb2cuY ...
- Android 设置背景透明度
一些时候,我们须要为UI页面设置背景色,例如以下图所看到的: 上图已注: 背景颜色为#000000,透明度为40%: 那么.怎样在代码中表示呢? 首先须要了解: 颜色和不透明度 (alpha) 值以十 ...
- LeetCode Algorithm 03_Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 优雅地使用Retrofit+RxJava(二)
前言 在我上一篇讲Retrofit+RxJava在MVP模式中优雅地处理异常(一)中,发现非常多网友发邮箱给我表示期待我的下一篇文章,正好趁着清明假期.我就写写平时我在使用RxJava+Retrofi ...