Pointfree编程风格(草稿)
如下示例1
2
3
4
5
6
7
8
9
10import { someAction } from "./action"
function mapStateToProps(state) {
return state.path.to.my.state;
}
function mapActionToProps(dispatch) {
return bindActionCreators({
goDetail: someAction
},dispatch)
}
connect(mapStateToProps, mapActionToProps)
可以用lodash像如下实现1
2
3
4
5
6
7import { someAction } from "./action"
import { property,partial } from "lodash"
const mapStateToProps = property("path.to.my.state")
const mapActionToProps = partial(bindActionCreators,{
goDetail: someAction
})
connect(mapStateToProps, mapActionToProps)
以下示例出自reselect1
2
3
4
5
6
7
8
9import { createSelector } from 'reselect'
const shopItemsSelector = state => state.shop.items
const taxPercentSelector = state => state.shop.taxPercent
const subtotalSelector = createSelector(
shopItemsSelector,
items => items.reduce((acc, item) => acc + item.value, 0)
)
改写后1
2
3
4
5
6
7
8
9import { createSelector } from 'reselect'
import { property,reduce,partialRight } from "lodash"
const shopItemsSelector = property("shop.items")
const taxPercentSelector = property("shop.taxPercent")
const subtotalSelector = createSelector(
shopItemsSelector,
partialRight(reduce,(acc, item) => acc + item.value,0)
)