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. php实现排列组合

    php实现排列组合 一.总结 1.回溯:回溯的函数参数有些生疏了,记录递归的位置(pos或step),还要有东西(vis数组)来记录这个是否已经被访问 2.php全局变量的使用 :外部定义的普通变量, ...

  2. 李笑来~执行力WWH

    什么是秘密 秘密是指只有极少数人知道的实用信息.这个实用信息可以为知道且懂得运用的人获得收益,这个收益可能包括钱.名声和快感. 什么是执行力 执行力=What + Why + How,即WWH 执行力 ...

  3. 在IE中opacity透明度

    body{ background: red; opacity: 0.5; filter:alpha(opacity=50); } jQuery: if($.support.opacity == tur ...

  4. Linux系统捕获数据包流程

    Linux系统捕获数据包流程 为了提高数据包的捕获效率,瓶颈问题是一个需要非常关注的焦点.减少在捕获数据包过程中的瓶颈,就能够提高数据包捕获的整体性能.下面本文将以Linux操作系统为平台,分析捕获数 ...

  5. 基于WebSphere与Domino的电子商务网站构架分析

    650) this.width=650;" border="0" alt="174812596.jpg" src="http://img1. ...

  6. 【Energy Forecasting】能源预測的发展和展望

    说明 本文的内容来自Tao Hong博士的Energy Forecasting: Past, Present and Future一文的翻译和整理. 引入 能源预測包括了电力行业中有关预測的广泛的内容 ...

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

  8. CentOS经常使用文件操作命令[百度博客搬家]

    路径操作的CentOS经常使用命令 如今整理例如以下(百度博客搬家)   cd pwd  NO1. 显示当前路径  [root@rehat root]# pwd  NO2. 返回用户主文件夹  [ro ...

  9. Web 组件是什么

    Web 组件是什么 一.总结 这篇下面的内容多看 1.组件化的目的:高内聚,低耦合,便于多人同时开发 2.各种前端框架(前端组件化)让写前端不要太简单: 3.组件编程最最最最简单实例: <lin ...

  10. Android实现微信分享及注意事项

    一.获取帮助文档并下载相关资料 首先打开微信开放平台:https://open.weixin.qq.com/ 如果没有注册,请先注册并上传开发者资料等待审核. 资源中心----移动应用开发----分享 ...