Redux实践(1)

2017-09-05  本文已影响98人  Syanbo

redux 新的尝试

  1. normalizr
  2. redux-actions
  3. redux-persist
  4. redux-thunk
  5. reselect

一些思考

connectComponent

给connect做一个中间层便于默认参数统一配置

import {connect} from "react-redux";
import {bindActionCreators} from "redux";
import actions from "./actions";

const options = {
    withRef: true
};
const defaultMapStateToProps = (state, props) => ({});
const defaultMapDispatchToProps = (dispatch, props) => ({
    actions: bindActionCreators(actions, dispatch)
});
const defaultMergeProps = (stateProps, dispatchProps, ownProps) => (
    Object.assign({}, ownProps, stateProps, dispatchProps)
);

const getMapStateToProps = (makeSelector) => () => (state, props) => ({
    data: makeSelector()(state, props),
});

export default ({makeSelector, mapStateToProps, mapDispatchToProps, mergeProps}) => (
    connect(
        mapStateToProps || makeSelector && getMapStateToProps(makeSelector) || defaultMapStateToProps,
        mapDispatchToProps || defaultMapDispatchToProps,
        mergeProps || defaultMergeProps,
        options
    )
);

reselect (选择器)

const makeSelector = () => createSelector(
    [
        (state, props) => {
            let {home, Goods, Users} = state;
            return denormalize(home.list, GoodsList, {Goods, Users})
        }
    ],
    (data) => data
);

normalizr

为什么使用
设计模式假设为后台与前台对应,前台维护自己的一套数据模型,应用数据架构复杂的情况下,后台数据不理想而产生的影响

方法
上一篇下一篇

猜你喜欢

热点阅读