[Recompose] Flatten a Prop using Recompose
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的更多相关文章
- [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 ...
- [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 ...
- [Recompose] Compute Expensive Props Lazily using Recompose
Learn how to use the 'withPropsOnChange' higher order component to help ensure that expensive prop c ...
- [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] When nesting affects Style
In CSS we use the descendant selector to style elements based on their nesting. Thankfully in React ...
- react recompose
避免写嵌套 import { compose } from "recompose"; function Message(props) { const { classes } = p ...
- [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] 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] Stream Props to React Children with RxJS
You can decouple the parent stream Component from the mapped React Component by using props.children ...
随机推荐
- Axios再记录
一个基于Promise 用于浏览器和 nodejs 的 HTTP 客户端(可实现ajax的请求) 有关学习网址:https://www.tuicool.com/articles/eMb2yuY ...
- 辛星跟您玩转vim第三节之程序猿特须要的移动方式
前面第二节我首先值得一提的是,我的vim教程pdf版本号已经写完了.大家能够去下载,这里是csdn的下载地址:csdn下载.假设左边的下载地址挂掉了.也能够自行在浏览器以下输入例如以下地址进行下载:h ...
- Android开源图表库XCL-Charts版本号公布及展示页
XCL-Charts V2.1 Android开源图表库(XCL-Charts is a free charting library for Android platform.) XCL-Charts ...
- D3.js加载csv和json数据
1.加载数据的基本命令 D3提供了方法可以对不同的数据类型进行加载,比如d3.text(), d3.xml(), d3.json(), d3.csv(), 和d3.html(). <!DOCTY ...
- php中类的持久化如何实现
php中类的持久化如何实现 一.总结 一句话总结:PHP持久化通过serialize() 和 unserialize() 这两个函数来实现的. 1.持久化之后的对象保存到哪里? 将复杂的数组之类 ...
- ORA-16047: DGID mismatch between destination setting and standby
主库有报错如下: ORA-16047: DGID mismatch between destination setting and standby 原因:主库参数设置错误,检查下列参数:log_arc ...
- Vue的学习--环境配置
1. 下载vue.min.js或者使用CDN 2. 安装Vue-cli环境 我在window7 32位下使用命令行cmd进行的操作 安装之前应该使用node -v和npm -v检查一下node和n ...
- BZOJ3529: [Sdoi2014]数表(莫比乌斯反演,离线)
Description 有一张 n×m 的数表,其第 i 行第 j 列(1 <= i <= n, 1 <= j <= m)的数值为 能同时整除 i 和 j 的所有自然数之和.给 ...
- Android 利用代码在屏幕中间位置显示ProgressDialog和ProgressBar
package cc.testprogressdialog; import android.os.Bundle; import android.view.Gravity; import android ...
- Appium_Java运行测试脚本时问题汇总
问题一.java.lang.NoClassDefFoundError: org/openqa/selenium/remote/SessionNotFoundExceptionCaused by: ja ...