[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 ...
随机推荐
- Revit API取得系统族普通族几何信息的方法
系统族,可以直接转化为对应的类(Wall,Duct)然后取得几何信息,普通族需要转化为FamilyInstance )) { TaskDialog.Show() ...
- Java嵌入式数据库H2学习总结(三)——在Web应用中嵌入H2数据库
H2作为一个嵌入型的数据库,它最大的好处就是可以嵌入到我们的Web应用中,和我们的Web应用绑定在一起,成为我们Web应用的一部分.下面来演示一下如何将H2数据库嵌入到我们的Web应用中. 一.搭建测 ...
- finger-guessing game:2增加游戏进度,界面title及游戏界面
增加游戏进度,界面title及游戏界面 //初始化引擎组件 init(50, "div_caiquan", 800, 400, main); //定义游戏层,加载进度层.游戏背景层 ...
- NSString 拼接字符串
NSString* string; // 结果字符串 NSString* string1, string2; //已存在的字符串,需要将string1和string2连接起来 //方法1. strin ...
- 技术人生:Knowing when or where it’s appropriate to use a technique or tool is just as important as knowing how to use it.
Knowing when or where it’s appropriate to use a technique or tool is just as important as knowing ho ...
- 基于Memcached的tomcat集群session共享所用的jar
多个tomcat各种序列化策略配置如下:一.java默认序列化tomcat配置conf/context.xml添加<Manager className="de.javakaffee.w ...
- git 拉取远程指定分支 pull本地不存在的分支
默认,git项目只有一个分支,就是master,我们当然可以在本地创建多个分支,并推送到远程git管理平台上,或者将远程git管理平台上的其他分支拉取到自己电脑上. 一.查看本地已有的分支 进入到项目 ...
- 【转】从源码来分析ListView
原文:http://yanmingming.sinaapp.com/?p=1251 原文其实不叫这个名字,本文对于原文有一定的修改,觉得这个名字比较适合本篇. 一.ListView 的工作原理 Ada ...
- Material Designer的低版本兼容实现(三)——Color
在Material Designer中,色彩再一次被摆到了重要的位置上.官方文档中竟然给出了500种配色方案进行选择.就是为了给不同的手机.电视.手表上带来一直的用户体验. 更多用于控制色彩的属性,可 ...
- sulime代理设置、插件管理
使用command palette或者package control,可以管理插件:安装.更新.启动.关闭插件.卸载插件等 配置Package Control 配置举例: { "bootst ...