[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: 4,
    todoFilter: ‘SHOW_ALL‘,
    todos: [
       ...
    ],
    ui: {
        filterGroups: {
            status: false
        }
    }
}

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

import { compose, curry, option, propPath } from ‘crocks‘
...

const activeGroup = curry(group =>
    compose(
        option(FilterButton.defaultProps.active),
        propPath([‘ui‘, ‘filterGroups‘, group])
    )
)
const mapStateToProps = (state, ownProps) => ({
    active: activeGroup(ownProps.group, state)
})

export default connect(mapStateToProps)(FilterButton)

--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 { compose, curry, option, propPath } 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),
        propPath([‘ui‘, ‘filterGroups‘, group])
    )
)

const mapStateToProps = (state, ownProps) => ({
    active: activeGroup(ownProps.group, state)
})

export default connect(mapStateToProps)(FilterButton)

原文地址:https://www.cnblogs.com/Answer1215/p/10727119.html

时间: 2024-08-01 17:50:04

[Functional Programming + React] Provide a reasonable default value for mapStateToProps in case initial state is undefined的相关文章

Beginning Scala study note(4) Functional Programming in Scala

1. Functional programming treats computation as the evaluation of mathematical and avoids state and mutable data. Scala encourages an expression-oriented programming(EOP) 1) In expression-oriented programming every statement is an expression. A state

Functional Programming.

I have been seeing Redux for sometime, but recently I come to realize that , it's part of so called functional programming. The basic idea for functional programming is so simple, Declare all dependency as function input, no hidden input ( we should

Functional Programming in Javascript 中文翻译 —— 目录和介绍

原著:[美] Dan Mantyla 目录 前言1 Javascript函数式编程的力量--举个例子2 函数式编程基础3 建立函数式编程环境4 在Javascript中实现函数式编程的技术5 类型理论6 高级主题以及Javascript的缺陷7 Javascript中的函数式和面型对象编程8 Javascript中的函数式和面型对象编程 关于翻译的这本书 现在市面上有两本专注于javascript函数式编程的书,一本是<Functional Javascript>(下文简称FJS), 另一本就

Apply Functional Programming Principles

Apply Functional Programming Principles Edward Garson FUNCTiONAL PROGRAMMiNG has recently enjoyed renewed interest from the mainstream programming community. Part of the reason is because emergent properties of the functional paradigm are well positi

Functional Programming Principles in ScalaScala函式编程原理 第一章笔记

所有non-trival编程语言都提供了 基本表达式(expression)去表示最简单的表达式 组合表达式的方法 抽象表达式的方法,也就是为表达式引入一个名字去引用它 substitional model 替代模型 sumOfSquares(3,2+2) sumOfSquares(3,4) square(3)+square(4) 9+square(4) 9+16 25 这种模型的核心概念是所有的表达式都规约成值,替代模型在lamada表达式中被形式化,构成了函数式编程的基石 substitio

Coursera公开课Functional Programming Principles in Scala习题解答:Week 2

引言 OK.时间很快又过去了一周,第一周有五一假期所以感觉时间绰绰有余,这周中间没有假期只能靠晚上加周末的时间来消化,其实还是有点紧张呢!后来发现每堂课的视频还有对应的课件(Slide).字幕(subtitles)可以下载,这样下载视频学习和在线学习就只差课程中间的Exercise了 Week 2主要讲函数,函数在Scala里是first-class citizen,可以在任意域内出现,这门课其实也是在借Scala来讲函数式编程原理.好了,不多说,进入习题解析. 这周的作业主要是使用函数来表示一

Coursera公开课Functional Programming Principles in Scala习题解答:Week 3

引言 这周的作业其实有点复杂,需要完成的代码有点多,有点绕.本周的课程主要讲了Scala中的类.继承和多态,作业也很好的从各个方面考察了课程的内容.作业题目工程主要需要完成的部分是TweetSet.scala这个文件中的内容,比较新潮,都是和推特相关.其中定义了一个抽象类TweetSet,以及其的两个子类Empty.NonEmpty,表示空集和非空集.非空集使用二叉树来表示,二叉树的根是一个Tweet类对象,表示一条推特(用天朝的话来说叫做"微博"),一条微博哦不,一条推特包含user

Functional Programming 资料收集

书籍: Functional Programming for Java Developers papers: Why Functional Programming Matters 在线课程: CSE341: Programming Languages (washington university) 课程主页: https://courses.cs.washington.edu/courses/cse341/13wi/ cousera主页: https://class.coursera.org/p

Sth about 函数式编程(Functional Programming)

今天开会提到了函数式编程,针对不同类型的百年城方式,查阅了一部分资料,展示如下: 编程语言一直到近代,从汇编到C到Java,都是站在计算机的角度,考虑CPU的运行模式和运行效率,以求通过设计一个高效的编程语言,作为人与计算机之间沟通的桥梁.因为计算机本质上是串行执行一个个指令流,因此编程语言也被设计为命令式编程(Imperative Programming),先算什么再算什么,怎么输入怎么计算怎么输出,全部由编程人员决定. 后来,大家发现冯·诺伊曼结构将数据和指令平等化的思想能够帮助我们更好地对