我认为,如果我使用Z function
在mapDispatchToProps
中创建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为空
但是,当我通过`redux-dev-tool'强制调度AlarmContentActions.increment()
的操作类型时,所有过程都进行得很好
我认为redux-dev-tool
做得很好意味着我做好redux store (action, reducers)
,对吧?
我认为我正确地制作了mapDispatchToProps
,对吗?
那我怎么了?