博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Recompose] Transform Props using Recompose --mapProps
阅读量:5213 次
发布时间:2019-06-14

本文共 1923 字,大约阅读时间需要 6 分钟。

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}) => 
{name} - status
;const UserList = ({users, status}) =>

{status} User

{ users && users.map((user, i) =>
) }
;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' },];

Active users

u.status === 'active') }/>

Inactive users

u.status === 'inactive') }/>

Pending users

u.status === 'pending') }/>

 

Now let's say we want to hide the implement detail, instead just showing three different components.

;

 

import React from 'react';import {mapProps} from 'recompose';const User = ({name, status}) => 
{name} - status
;const UserList = ({users, status}) =>

{status} User

{ users && users.map((user, i) =>
) }
;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;

 

转载于:https://www.cnblogs.com/Answer1215/p/6854732.html

你可能感兴趣的文章
Maven 介绍
查看>>
hdu 4268
查看>>
启动tomcat时cmd窗口一闪而过
查看>>
两个有序数列,求中间值 Median of Two Sorted Arrays
查看>>
vue路由的实现原理
查看>>
Java核心技术:Java异常处理
查看>>
Python 学习笔记一
查看>>
引入列表,将对话分类添加到对应列表中
查看>>
回文子串
查看>>
Count Numbers
查看>>
React——JSX
查看>>
编写高质量代码改善C#程序的157个建议——建议110:用类来代替enum
查看>>
最大公约数求解
查看>>
网卡bond技术
查看>>
UITabbarController的UITabbarItem(例:"我的")点击时,判断是否登录
查看>>
机器学习之支持向量机(一):支持向量机的公式推导
查看>>
对【SQL SERVER 分布式事务解决方案】的心得补充
查看>>
UNIX基础知识之输入和输出
查看>>
Diango路由映射FBV和CBV
查看>>
Android Studio配置指南总结
查看>>