react redux - connect

2017-05-11  本文已影响83人  LYF闲闲闲闲

1. 使用方法

import React from 'react'
import Home from '../components/Home';
import {connect} from 'react-redux';

const mapStateToProps = (state) =>{
    console.log("state.number:",state.homeReducer.number);
    return {
        number:state.homeReducer.numbers
    }
};
const mapDispatchToProps = (dispatch)=> {
    return {
        add:(text)=>{
            dispatch({"ADD":text})
        }
    }
};
export default connect(mapStateToProps,mapDispatchToProps)(Home);

2. 作用

connect是一个高阶函数,首先传入mapStateToProps,mapDispatchToProps,然后返回一个生产Component的函数(wrapWithConnect),然后再将真正的Component作为参数传入wrapWithConnect(MyComponent),这样就生产出一个经过包裹的Connect组件

3. connect参数

connect(mapStateToProps, mapDispatchToProps, mergeProps, options = {})

上一篇下一篇

猜你喜欢

热点阅读