[Functional Programming + React] Provide a reasonable default value for mapStateToProps in case initial state is undefined
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)
[Functional Programming + React] Provide a reasonable default value for mapStateToProps in case initial state is undefined的更多相关文章
- BETTER SUPPORT FOR FUNCTIONAL PROGRAMMING IN ANGULAR 2
In this blog post I will talk about the changes coming in Angular 2 that will improve its support fo ...
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
- a primary example for Functional programming in javascript
background In pursuit of a real-world application, let’s say we need an e-commerce web applicationfo ...
- Java 中的函数式编程(Functional Programming):Lambda 初识
Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...
- Functional Programming without Lambda - Part 2 Lifting, Functor, Monad
Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...
- Functional Programming without Lambda - Part 1 Functional Composition
Functions in Java Prior to the introduction of Lambda Expressions feature in version 8, Java had lon ...
- Functional programming
In computer science, functional programming is a programming paradigm, a style of building the struc ...
- Functional programming idiom
A functional programming function is like a mathematical function, which produces an output that typ ...
- 关于函数式编程(Functional Programming)
初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...
随机推荐
- [golang 易犯错误] golang 局部变量初始化:=的陷阱
我们知道,golang中局部变量初始化方法(使用“:=”创建并赋值),让我们在使用变量时很方便.但是,这也是易犯错误的地方之一.特别是这个初始化符还支持多个变量同时初始化,更特别的是它还支持原有变量赋 ...
- WCF消息传递
通过了解了WCF的一些基本概念并创建和编写WCF应用中的相应方法,实现了WCF服务和客户端之间的调用,就能够理解WCF应用是如何进行通信的.了解了一些基本的WCF概念后,还需要深入了解WCF消息的概念 ...
- Mysql5.6主从复制-基于binlog
MySQL5.6开始主从复制有两种方式:基于日志(binlog):基于GTID(全局事务标示符). 此文章是基于日志方式的配置步骤 环境: master数据库IP:192.168.247.128sla ...
- Blocks Programming Topics
最近的工作中比较频繁的用到了Block,不在是以前当做函数指针的替代或者某些API只有Blocks形式的接口才不得已用之了,发现自己对其了解还是太浅,特别是变量的生存期,按惯例还是翻译官方文档,原文链 ...
- Scala从零開始:使用Intellij IDEA写hello world
引言 在之前的文章中,我们介绍了怎样使用Scala IDE也就是eclipse中集成的Scala开发插件来进行Scala语言程序的开发,在使用了一段时间之后,发现eclipse对Scala的支持并非非 ...
- 较老版本 AFNetworking 使用心得
较老版本的 AFNetworking 下载链接 http://pan.baidu.com/s/14Cxga 将压缩包中的文件夹拖入xcode工程项目中并引入如下的框架 简单的 JOSN 解析例子 ...
- Android之多种Bitmap效果
1. 将图片变为圆角 2. 获取缩略图图片 3. LOMO特效 4. 旧时光特效 5. 暖意特效 6. 根据饱和度.色相.亮度调整图片 7. 添加图片外边框 8. 添加内边框 9. 创建一个缩放的图片 ...
- Android源码中final关键字的用法及final,finally,finalize的区别
Android开发的学习流程 final,finally,finalize的区别 Android的发展越来越快,Android开发人员越来越多,当两种情况碰撞,在诸多开发者中跟紧Android步伐脱颖 ...
- pip 安装错误 'ascii' codec can't encode characters
安装 python-dev既可解决 apt-get install python-dev
- 《MATLAB面向对象程序设计》
<MATLAB面向对象程序设计> 基本信息 作者: 苗志宏 马金强 出版社:电子工业出版社 ISBN:9787121233449 上架时间:2014-6-18 出版日期:2014 年 ...