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的更多相关文章

  1. [Recompose] Lock Props using Recompose -- withProps

    Learn how to use the ‘withProps’ higher order component to pre-fill a prop, unable to be overridden. ...

  2. [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 ...

  3. [Recompose] Stream Props to React Children with RxJS

    You can decouple the parent stream Component from the mapped React Component by using props.children ...

  4. 乘风破浪,遇见Android Jetpack之Compose声明式UI开发工具包,逐渐大一统的原生UI绘制体系

    什么是Android Jetpack https://developer.android.com/jetpack Android Jetpack是一个由多个库组成的套件,可帮助开发者遵循最佳做法.减少 ...

  5. [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 ...

  6. [Recompose] Compute Expensive Props Lazily using Recompose

    Learn how to use the 'withPropsOnChange' higher order component to help ensure that expensive prop c ...

  7. recompose mapProps

    mapProps介绍 mapProps函数接收一个函数参数,这个函数参数会返回一个对象用作为接下来的组件的props.组件接收到的props只能是通过mapProps函数参数返回的对象,其他的prop ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 怎样扩展Chromium各层的接口

    加入新功能时,可能须要添加各层的接口,接口怎样加?必定须要向Chromium的原则看齐. 首先Chromium的模块设计遵循依赖倒置原则,上层模块依赖于低层模块.低层模块不会依赖上层模块的实现. 再者 ...

  2. sql查看依赖关系

    select OBJECT_NAME(object_id) as name,object_NAME(referenced_major_id) as ref from sys.sql_dependenc ...

  3. JS实践与写博客-序

    大二的时候,就开始接触JavaScript了. 当时学了1年多,主要是认真看了一本JavaScript的入门书籍,了解了JavaScript大致怎么回事.在独自做Web项目的时候,用的都是JavaSc ...

  4. 【2017 Multi-University Training Contest - Team 7】Just do it

    [Link]:http://acm.hdu.edu.cn/showproblem.php?pid=6129 [Description] 设定b[i]=a[1]^a[2]^a[3]^------a[i] ...

  5. GraphX 图数据建模和存储

    背景 简单分析一下GraphX是怎么为图数据建模和存储的. 入口 能够看GraphLoader的函数. def edgeListFile( sc: SparkContext, path: String ...

  6. 1.html+css页面设计

    转自:http://www.cnblogs.com/ywang/archive/2014/04/16/3668638.html 今天用html+css做一个最简单的页面.效果图如下: 说明:这里的韩文 ...

  7. event事件对象 兼容写法:var oEvent=ev||event;和取消事件冒泡

    要在整个页面添加事件要使用document,例如要捕抓鼠标位置 document.onclink=function(ev)  //FireFox Chrome默认都是有一个值传进来的 { var oE ...

  8. 让单选input框,不在被选中,添加disabled即可。输入框input的一些技巧

    1.让单选input框,不在被选中,添加disabled即可 2.input的file文件对象的清空,只需要input.val("");就可以了.

  9. 基于cropper.js的图片上传和裁剪

    项目中要求图片上传并裁剪的功能,之前也有接触过很多图片裁剪插件,效果体验不是很好,今天推荐一款好用的插件-cropper,超级好用,裁剪功能丰富,满足了各种需求. 功能: 1:点击选择图片,弹出文件夹 ...

  10. 【Codeforces Round #447 (Div. 2) B】Ralph And His Magic Field

    | [链接] 我是链接,点我呀:) [题意] 给你一个n*m矩阵,让你在里面填数字. 使得每一行的数字的乘积都为k; 且每一列的数字的乘积都为k; k只能为1或-1 [题解] 显然每个位置只能填1或- ...