触发mapDispatchToProps,但不会更改道具

时间:2018-10-24 09:20:49

标签: reactjs redux react-redux redux-thunk

我认为,如果我使用Z functionmapDispatchToProps中创建dispatch
    然后,我可以将Z function用作this.props.Z function。是吗?

MainPage.js

import React, {Component} from 'react'
import ReactDOM from 'react-dom'
import {connect, Provider} from 'react-redux'  

import store from '../../store'
import * as AlarmContentActions from '../../store/modules/AlarmContent'

class MainPage extends Component {

    constructor(props) {
        super(props)

    }

    showProps = () => {
        console.log(this.props)
    }

    render() {
        console.log(AlarmContentActions.increment())
        console.log(this.props)
        return(
            <>
                <button onClick={this.showProps}>here it is</button>
            </>
        )
    }
}



const mapDispatchToProps = (dispatch) => ({
    increment: () => {
        dispatch(AlarmContentActions.increment())
    }
  })


export default connect(null, mapDispatchToProps)(MainPage)

ReactDOM.render(
<Provider store={store}>
    <MainPage />
</Provider>, document.getElementById('main-page'))

AlarmContent.js (鸭子结构包括redux-actions和reducers)

import {createAction, handleActions} from 'redux-actions'


const initialState = {
      num: 0
}

//define action types
const INCREMENT = 'AlarmContent/INCREMENT';

//CREATE action
export const increment = createAction(INCREMENT);

//CREATE reducers
export default handleActions({

    [INCREMENT] : (state, action) => {
        return {
            num : state.num + 1
        } 
     }
}, initialState)

我制作了函数increment(),该函数在 MainPage.js 上分配了动作类型AlarmContentActions.increment(),但是props为空

enter image description here enter image description here

但是,当我通过`redux-dev-tool'强制调度AlarmContentActions.increment()的操作类型时,所有过程都进行得很好

enter image description here

  1. 我认为redux-dev-tool做得很好意味着我做好redux store (action, reducers),对吧?

  2. 我认为我正确地制作了mapDispatchToProps,对吗?

那我怎么了?

0 个答案:

没有答案