Learn how to use the ‘flattenProp’ higher order component to take a single object prop and spread each of its fields out as a prop.

For example,we have a 'redux-react' like app:

import React from 'react';
import { withProps } from 'recompose'; // Mock Configuration
function ReactRedux() {
const state = {
name: 'Joe',
status: 'active'
};
return {
connect: (map) => withProps(map(state))
};
} const {connect} = ReactRedux(); const UserStyle = {
position: 'relative',
background: 'lightblue',
display: 'inline-block',
padding: '10px',
cursor: 'pointer',
marginTop: '50px'
}; const mapStateToProps = (state) => ({
name: state.name,
status: state.status
}); let User4 = ({ status, name }) => (
<div style={UserStyle}>
{name} - {status}
</div>
); User4 = connect(mapStateToProps)(User4); export default User4;

Here we using 'connect' & 'mapStateToProps'. 'connect' is also a HoC.

import React from 'react';
import { withProps, compose, flattenProp } from 'recompose'; // Mock Configuration
function ReactRedux() {
const state = {
user: {
name: 'Joo',
status: 'inactive'
},
address: {
street: 'SF',
postcode: ''
}
};
return {
connect: (map) => withProps(map(state))
};
} const {connect} = ReactRedux(); const UserStyle = {
position: 'relative',
background: 'lightblue',
display: 'inline-block',
padding: '10px',
cursor: 'pointer',
marginTop: '50px'
}; const mapStateToProps = (state) => ({
user: state.user,
address: state.address
}); const enhance = compose(
connect(mapStateToProps),
flattenProp('user')
); let User4 = enhance(({ name, status, address }) => (
<div style={UserStyle}>
{name} - {status} - {address.street} - {address.postcode}
</div>
)); export default User4;

'flattenPorp' helps to get single prop from object and spread its props.

[Recompose] Flatten a Prop using Recompose的更多相关文章

  1. [Recompose] Set the HTML Tag of a Component via a Prop using Recompose

    Learn how to user the ‘componentFromProp’ helper and ‘defaultProps’ higher order component to swap t ...

  2. [Recompose] Pass a React Prop to a Stream in RxJS

    When you declare your Component and Props in JSX, you can pass those props along to your RxJS stream ...

  3. [Recompose] Compute Expensive Props Lazily using Recompose

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

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

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

  5. [Recompose] When nesting affects Style

    In CSS we use the descendant selector to style elements based on their nesting. Thankfully in React ...

  6. react recompose

    避免写嵌套 import { compose } from "recompose"; function Message(props) { const { classes } = p ...

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

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

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

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

随机推荐

  1. 利用hibernateTemplate进行最简单的分页

    安全的方法例如以下.别用Session s=getSession()........ /**  * 使用hql 语句进行操作  * @param hql HSQL 查询语句  * @param off ...

  2. 「微信小程序」有哪些冲击与机会?

    昨天晚上相信大家的朋友圈被「微信小程序」刷屏了,这影响力赶上了国务院出台新政策一样,足以说明微信在中国的影响力之大. 然后今天公号后台一大堆人问我怎么看这件事,不少人非常忧虑,仿佛自己将要失业一样. ...

  3. 杭电(hdu)2053 Switch Game 水题

    Switch Game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  4. [NOIP2013]车站分级 解题报告

    妈蛋这道普及组水(神)题搞了我非常久. 一. 首先一个非常显然的事情就是每一个火车告诉了站与站之间的等级关系,所以拓扑求最长路. 可是发现暴力建边的话最坏能够达到500*500,所以时间复杂度有O(M ...

  5. Catch Me If You ... Can't Do Otherwise--转载

    原文地址:https://dzone.com/articles/catch-me-if-you-cant-do-otherwise I don't know whether it's an anti- ...

  6. JavaScript--数据结构与算法之排序

    排序总结————常见的排序 常见的9中排序(冒泡,选择,插入(二分插入,希尔),归并,快速,堆,计数,基数,桶排序)可分为两类 比较排序:冒泡,选择,插入(二分插入,希尔),归并,堆,快速 非比较排序 ...

  7. pytest_多用例执行(1)

    一.首先创建测试套件 # -*- coding:utf-8 -*-from __future__ import print_functionimport pytestimport allure cla ...

  8. 解决使用SecureCRT不能连接Ubuntu的问题

    一.现象 SecureCRT是远程登陆工具及串口,可以远程进行登陆Linux服务器或者串口打印数据.但我下载安装了之后想通过SecureCRT来远程登陆我的Ubuntu,出现一直连接不上. 二.问题原 ...

  9. 【2017 Multi-University Training Contest - Team 7】Hard challenge

    [Link]:http://acm.hdu.edu.cn/showproblem.php?pid=6127 [Description] 平面上有n个点,每个点有一个价值,每两个点之间都有一条线段,定义 ...

  10. 设计模式六大原则(四):接口隔离原则(Interface Segregation Principle)

    接口隔离原则(ISP)定义: 客户端不应该依赖它不需要的接口:一个类对另一个类的依赖应该建立在最小的接口上. 问题由来: 类A通过接口I依赖类B,类C通过接口I依赖类D,如果接口I对于类A和类B来说不 ...