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. python RESTful API框架:Eve 高速入门

    Eve是一款Python的REST API框架.用于公布高可定制的.全功能的RESTful的Web服务.帮你轻松创建和部署API,本文翻译自Eve官方站点: http://python-eve.org ...

  2. Android启动原理剖析

    我们知道Android是以一个Activity为单位的,可是我们并没有看到一个Activity是怎么開始启动的. 今天我 们就从Android的源码開始讲吧. ActivityThread: Andr ...

  3. jquery的图片轮播 模板类型

    先说一下用到的几个重要的事件 j jQuery on()方法是官方推荐的绑定事件的一个方法. $(selector).on(event,childSelector,data,function,map) ...

  4. Qt样式表——选择器详解(父子关系)

    在上一节中,小豆君给大家介绍了样式表的基本概念和大致用法.今天我们来详细了解下样式表中选择器的用法. 所谓选择器,就是指定你所设置的样式对哪个或哪些控件起作用. 到目前为止,Qt样式表支持CSS2中定 ...

  5. int android.support.v7.widget.RecyclerView$ViewHolder.mItemViewType' on a null.....

    Android.support.v7.widget.RecyclerView$ViewHolder.mItemViewType' on a null..空指针问题,费劲心思才找到报空指针的原因: 代码 ...

  6. ArcGIS小技巧——提取面要素的质心点

    如下图,现在要做这样一件事,提取面图层中每一个图斑的质心点,然后使用质心点提取图层中的一个属性值,并在此基础上进行克里金插值,生成该属性的空间插值图.当然,今天这段文字主要简单说一下怎样提取面图层的质 ...

  7. [TS] Implement a singly linked list in TypeScript

    In a singly linked list each node in the list stores the contents of the node and a reference (or po ...

  8. Hibernate之API初识及增删改查实现

    声明:关于hibernate的学习.非常大一部分东西都是概念性的. 大家最好手里都有一份学习资料,在我的博文中.我不会把书本上的概念一类的东西搬过来.那没有不论什么意义.关于hibernate的学习, ...

  9. 【CS Round #46 (Div. 1.5) B】Letters Deque

    [链接]h在这里写链接 [题意] 让你把一个正方形A竖直或水平翻转. 问你翻转一次能不能把A翻转成B [题解] 有说一定要恰好为1次. 并不是说A和B相同就一定不行. [错的次数] 2 [反思] 自己 ...

  10. 观察者模式 VS 责任链模式

    为什么要把观察者模式和责任链模式放在一起对比呢?这两个模式没有太多的相似性呀,真没有嘛?有相似性,我们在观察者模式中也提到了触发链(也叫做观察者链)的问题,一个具体的角色既可以是观察者,也可以是被观察 ...