I am trying to clean up my mapStateToProps, using ramda I can just use r.pick to return a curried function by passing in my desired attrs like so:
const mapStateToProps = R.pick(['board', 'nextToken'])
Redux will then pass in the state to the returns function.
What I want to do is modify the state first, because my state is an immutable map, so i need to pass pick my state cast as plan js.
I can do this with the following but i am convinced that there is a cleaner way of doing this, functionally. Is it something simple that anybody can see?
import {Map} from 'immutable'
import R from 'ramda'
const immutable_state = Map({
name: 'alice',
age: 30
})
const pick_from_immutable = (attrs, obj) =>
// accepts a list of attrs
// returns a function that accepts an immutable and returns its attrs defined by attrs, also be nice to just return the value if called with an immutable 2nd arg
obj ?
R.pick(attrs, obj.toJS()) :
(imm) => R.pick(attrs, imm.toJS())
const pick_age_from_immutable = pick_from_immutable(['age'])
console.log(pick_age_from_immutable(immutable_state)) // -> { age: 30 }
const mapStateToProps = pick_age_from_immutable
I prefer to use Immutablejs functions for these kinds of things. It works fine with nested items. But I can see a use case if I am picking age (or some property) in multiple components then I might create a utility function for it.
Example:
var a = Map({
b: Map({
c: 10
})
})
// Get value of c in nested a object. Set not found value to null.
var valueOfC = a.getIn(['b', 'c'], null)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.