For example we have a component, it needs to call 'react-redux' connect function.

import { compose, curry, option, propPath } from '../js/helper'

const FilterButton = ({ active, onClick }) => {
const classes = classnames('filterButton', {
'filterButton--active': active
})
return <Button className={classes} onClick={onClick} icon={faFilter} />
} FilterButton.defaultProps = {
active: true,
onClick: Function.prototype
} FilterButton.propTypes = {
active: PropTypes.bool,
group: PropTypes.string.isRequired,
onClick: PropTypes.func
} const mapStateToProps = (state, ownProps) => ({
active: state.ui.filterGroups[ownProps.group]
}) export default connect(mapStateToProps)(FilterButton)

For the hightlighted part, there can be many possible reason for it to go wrong. We can use Maybe to provide a reasonable default value.

First, the inital State is:

const data = {
nextId: ,
todoFilter: 'SHOW_ALL',
todos: [
...
],
ui: {
filterGroups: {
status: false
}
}
}

If the highlighted part is undefined, we still want it works.

import { chain, compose, curry, isBoolean, option, propPath, safe } from 'crocks'
... const activeGroup = curry(group =>
compose(
option(FilterButton.defaultProps.active),
chain(safe(isBoolean)),
propPath(['ui', 'filterGroups', group])
)
)
const mapStateToProps = (state, ownProps) => ({
active: activeGroup(ownProps.group, state)
}) export default connect(mapStateToProps)(FilterButton)

The reason to put a chain() there is because, if the initial state is not boolean but string value, we can do safe type check, to make sure, what we got is Boolean value in the end, it not a Boolean, then Option() will set it to false.

--Full code--

import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import Button from './controls/Button'
import classnames from 'classnames'
import { faFilter } from '@fortawesome/free-solid-svg-icons' import { chain, compose, curry, isBoolean, option, propPath, safe } from 'crocks' const FilterButton = ({ active, onClick }) => {
const classes = classnames('filterButton', {
'filterButton--active': active
})
return <Button className={classes} onClick={onClick} icon={faFilter} />
} FilterButton.defaultProps = {
active: true,
onClick: Function.prototype
} FilterButton.propTypes = {
active: PropTypes.bool,
group: PropTypes.string.isRequired,
onClick: PropTypes.func
} const activeGroup = curry(group =>
compose(
option(FilterButton.defaultProps.active),
chain(safe(isBoolean)),
propPath(['ui', 'filterGroups', group])
)
) const mapStateToProps = (state, ownProps) => ({
active: activeGroup(ownProps.group, state)
}) export default connect(mapStateToProps)(FilterButton)

最新文章

  1. 【开源】OSharp3.0框架解说系列:新版本说明及新功能规划预览
  2. nodejs-2:模块与包管理工具
  3. Activity设置全屏的三种方法
  4. CentOS下httpd下php 连接mysql 本机可以,127.0.0.1不能访问
  5. XML的DOM、SAX、DEMO4J及DEMO4J整合Path的代码例子
  6. C和指针 (pointers on C)——第三章——数据
  7. iOS打包静态库(完整篇)
  8. SQL---索引---创建索引
  9. 下载工具axel 和 mwget
  10. LDA-线性判别分析(二)Two-classes 情形的数学推导
  11. Luogu3613 睡觉困难综合征/BZOJ4811 Ynoi2017 由乃的OJ 树链剖分、贪心
  12. vue-cli之webpack的proxyTable无效的解决方案
  13. OpenCV几种访问cv::Mat数据的方法
  14. C#中byte[] 转 double[] 或 int[] 或 struct结构体
  15. go语言生成uuid
  16. linux分区详解
  17. Luogu P3251 [JLOI2012]时间流逝 期望dp
  18. mysql拼接字符串和过滤字符的方法
  19. 《ArcGIS Runtime SDK for Android开发笔记》——翻译:ArcGIS Runtime SDK for Android 10.2.7发布
  20. Android系统篇之—-编写系统服务并且将其编译到系统源码中【转】

热门文章

  1. HDU 4733 G(x) (2013成都网络赛,递推)
  2. DELPHI - How to use opendialog1 for choosing a folder? TOpenDialog, TFileOpenDialog
  3. How to detect the types of executable files
  4. Go内置库模块 flag
  5. InnoDB 与 MYISAM
  6. 利用MPMoviePlayerViewController 播放视频 iOS
  7. 【java】java下载文件中换行符 在windows和linux下通用的
  8. Android按返回键退出程序但不销毁,程序后台运行,同QQ退出处理方式
  9. WordPress主题开发:WP_Query使用分页实例
  10. 网易游戏2015年暑期实习生面试经历-游戏研发project师