[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 ...
随机推荐
- 移动GPU全解读(二)
[编者按]:本文作者为爱搞机特约作者.技术达人"炮神"@ioncannon. 在上一篇移动GPU解读中,对移动GPU的架构.相关參数进行了介绍,本部分介绍的则是移动GPU的Shad ...
- Android中的MVP架构初探
说来羞愧,MVP的架构模式已经在Android领域出现一两年了.可是到今天自己才開始Android领域中的MVP架构征程. 闲话不多说,開始吧. 一.架构演变概述 我记得我找第一份工作时,面试官问我& ...
- 6.Windows 二进制文件 (.exe)安装--终端安装
转自:http://www.runoob.com/nodejs/nodejs-tutorial.html 32 位安装包下载地址 : http://nodejs.org/dist/v0.10.26/n ...
- [Javascript AST] 1. Continue: Write a simple Babel plugin
We want to write a Babel Plugin, which move 'const versionRegex = /(/d+)\.(/d+)\.(/d+)/gi' out of fu ...
- PHP版本 D-Link 动态域名客户端
<?php /* * D-Link 动态域名客户端.主域名www.dlinkddns.com 和 www.dlinkddns.com.cn * 首先获取外网IP,若IP没有变化,则结束运行:否则 ...
- Android代码模拟物理、屏幕点击事件
一.应用中模拟物理和屏幕点击事件 例如,模拟对某个view的点击事件 private void simulateClick(View view, float x, float y) { long do ...
- python项目实战-小游戏1
项目规则: 1.玩家和敌人分别从现有的角色中选择3个角色 2.随机生成目前的血量,和攻击量 3.游戏规则:当玩家向敌人发起攻击,敌人当前的血量=之前的血量-玩家的血量,同理 4.3局两胜 5.自定义玩 ...
- vb.net 调用api
Public Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Integer) ...
- mycat 之datanode datahost writehost readhost 区别(转)
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> &l ...
- 在设置了android:parentActivityName后,点击子Activity返回键,父Activity总会调用OnDestroy()的解决方式
近期查了非常久这个事情.分享给大家, 原理非常easy,一个Activity在manifet里声明了android:parentActivityName:这时候通过Activity左上角的返回butt ...