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. cogs P1578【模板】 次小生成树初级练习题

    1578. 次小生成树初级练习题 ☆   输入文件:mst2.in   输出文件:mst2.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 求严格次小生成树 [输入格式 ...

  2. nyist oj 1058 部分和问题 (DFS搜索)

    部分和问题 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 给定整数a1.a2........an.推断能否够从中选出若干数,使它们的和恰好为K. 输入 首先,n和k ...

  3. 关于Webpack详述系列文章 (第二篇)

    1.缩小文件搜索范围 1.1.1 include & exclude module:{ rules:[ { test:/\.js$/, use:['babel-loader?cacheDire ...

  4. 手动挂接NFS

     环境: 单板:s3c2440 内核:Linux-2.6.22.6 U-boot1.16 初始根文件系统Yaffs2 前提条件 1. 开发板上要烧写好文件系统 2. 能正常开机进入Linux系统 3. ...

  5. 解决sublime text3配置Python3编译环境:运行代码时提示“NO Build System”

    只需要在路径中把单杠换成双杠,重启sublime即可.

  6. jsp中标签id和name的区别(转)

    name原来是为了标识之用,但是现在根据规范,都建议用id来标识元素. 但是name在以下用途是不能替代的:1. 表单(form)的控件名,提交的数据都用控件的name而不是id来控制.因为有许多na ...

  7. 9. ZooKeeper之搭建单机模式。

    转自:https://blog.csdn.net/en_joker/article/details/78673456 在集群和单机两种模式下,我们基本完成了分别针对生产环境和开发环境ZooKeeper ...

  8. 学习笔记:Vue——插槽

    关于Vue插槽,只用过最简单的语法,现在完整地走一遍官方文档说明,并且探索更多用法. 01.如果组件中没有包含一个<slot>元素,则该组件起始标签和结束标签之间的任何内容都会被抛弃. 0 ...

  9. 【CS Round #46 (Div. 1.5) E】Ultimate Orbs

    [链接]链接 [题意] n个人从左到右站在一条直线上.每个人都有一个能力值g[i],然后每个人可以将相邻的一个人打败. 然后它的能力值能够增加相应的能力值(就是打败了的那个人的能力值). A能够打败B ...

  10. [Angular] Update FormArray with patchValue

    Currently, patchValue doesn't support update FormArray. The workarround is you need to empty the for ...