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. [Winform]js与webbrowser交互

    摘要 目前项目中采用的方式是内嵌浏览器的方式,打开本地或者互联网上的h5页面.在开发之前做了一下调研.目前常用的在C#封装的浏览器内核中,Chromium 内核封装有Xilium.Cefglue.Ce ...

  2. Setting an Event to Null

    I have a code like this: public class SomeClass { int _processProgress; public int ProcessProgress { ...

  3. UITabBar 详解

    1.push时,将tabar隐藏,方法1,在push之前,加入如下代码: -(IBAction)btnOnClicked:(id)sender { SQVideoListViewController ...

  4. self.xxx = nil 可以等效于[_xxx release] _xxx= nil 么

    如果属性是copy.retain的话是等价的.如下: - (void)setXXX:(NSString*)axx { if (_xxx != axx) { [_xxx release]; _xxx = ...

  5. 【spring boot】spring boot 前台GET请求,传递时间类型的字符串,后台无法解析,报错:Failed to convert from type [java.lang.String] to type [java.util.Date]

    spring boot 前台GET请求,传递时间类型的字符串,后台无法解析,报错:Failed to convert from type [java.lang.String] to type [jav ...

  6. IntentService 与ResultReceiver

    from://http://lyzhanghai.iteye.com/blog/947504 在google的I/O大会中关于“Writing zippy Android apps”,有讲过用Inte ...

  7. 算法java实现--动态规划--电路布线问题

    /* * dianlubuxian.java * Version 1.0.0 * Created on 2017年11月30日 * Copyright ReYo.Cn */ package reyo. ...

  8. Java Swing 使用非本地字体

    package reyo; import java.awt.Container; import java.awt.Font; import java.awt.FontFormatException; ...

  9. Vector HashMap List 存取数据速度

    数组大小:40000List_List:0.0045List :0.0818List_HashMap:0.0072HashMap :0.0517List_Vector:0.0037Vector :0. ...

  10. 一步一步做出属于自己的Eclipse

    本文将教大家一步一步打造属于自己的eclipse,涉及到地方,不完全之处请谅解. 一.下载 进入eclipse网站:http://www.eclipse.org/downloads/ 下载:Eclip ...