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的更多相关文章

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

  2. Beginning Scala study note(4) Functional Programming in Scala

    1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...

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

  4. Java 中的函数式编程(Functional Programming):Lambda 初识

    Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...

  5. Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

    Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...

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

  7. Functional programming

    In computer science, functional programming is a programming paradigm, a style of building the struc ...

  8. Functional programming idiom

    A functional programming function is like a mathematical function, which produces an output that typ ...

  9. 关于函数式编程(Functional Programming)

    初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...

随机推荐

  1. HDU 3976 Electric resistance (高斯消元法)

    Electric resistance Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  2. poj 2429 GCD &amp; LCM Inverse 【java】+【数学】

    GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9928   Accepted:  ...

  3. 护士当家第一至七季/全集Nurse Jackie迅雷下载

    英文译名Nurse Jackie,第1-7季(2009-2015)Showtime. 本季看点:<护士当家>是一部黑色医务剧,该剧特别邀请到荣获艾美奖和金球奖的Edie Falco出演女主 ...

  4. ios之调用打电话,发短信,打开网址

    1.调用 自带mail [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://admin@hz ...

  5. Python集合模块collections

    collections collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtuple 我们知道tuple可以表示不变集合,例如,一个点的二维坐标就可以表示成: ...

  6. [转]Redis作者:深度剖析Redis持久化

    From : http://www.iteye.com/news/24675 Redis是一种面向“key-value”类型数据的分布式NoSQL数据库系统,具有高性能.持久存储.适应高并发应用场景等 ...

  7. python的重试库tenacity用法以及类似库retry、requests实现

    介绍 tenacity is an Apache 2.0 licensed general-purpose retrying library, written in Python, to simpli ...

  8. python模块uuid产生唯一id

    使用版本4:uuid4就可以了 UUID4缺点:糟糕的随机数发生器使得它更有可能发生碰撞,但是概率真的很小 UUID1缺点:暴露隐私 If all you want is a unique ID, y ...

  9. 请求被中止: 未能创建 SSL/TLS 安全通道,设置 TLSv1.2和TLSv1.1版本 .基础链接已经关闭,发送时发生错误

    WSO2 API访问的安全要求, 只能提供TLSv1.2和TLSv1.1版本,其它SSL版本协议因为存在较高安全漏洞问题会被disable. A 改成TLSv1.1或TLSv1.2,最好使用TLSv1 ...

  10. dubbo源码解析-spi(3)

    前言 在上一篇的末尾,我们提到了dubbo的spi中增加了IoC和AOP的功能.那么本篇就讲一下这个增加的IoC,spi部分预计会有四篇,因为这东西实在是太重要了.温故而知新,我们先来回顾一下,我们之 ...