嗨@jaredpalmer ,开始使用您的入门项目开展项目。 做得好!
我有一个关于在用户交互(例如按钮单击)上调度 redux 操作的问题。 我的问题更多是关于正确的语法。 在过去,我调用了构造函数并像这样调用了绑定函数:
function mapDispatchToProps(dispatch) {
return bindActionCreators({
importedAction,
}, dispatch);
}
class Calendar extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick(programId) {
this.props.importedAction()
.then((response) => {
do something with response
});
}
}
....
render() {
return (
<a onClick={this.handleClick}>Click me to dispatch action</a>
)
}
我认为您安装了一个 babel-plugin,它可以让您避免一直编写“扩展 React.Component”。 我也看不到“bindActionCreators”。
问题,我将如何在您的入门项目中使用它?
function mapDispatchToProps(dispatch) {
return bindActionCreators({
importedAction,
}, dispatch);
}
class Calendar extends React.Component {
handleClick(programId) {
this.props.importedAction()
.then((response) => {
do something with response
});
}
}
....
render() {
return (
<a onClick={(evt) => this.handleClick()}>Click me to dispatch action</a>
)
}
好像您没有使用connect
react-redux
:
从'react-redux'导入{连接}
...您的组件...
导出默认连接(mapStateToProps,actionCreators)(日历)
关闭它,因为它与样板无关。 不过,我建议观看 Dan Abramov 关于 Redux 的课程:
另请查看connect
的react-redux
API: https ://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops -mapdispatchtoprops-mergeprops-options
最有用的评论
好像您没有使用
connect
react-redux
:从'react-redux'导入{连接}
...您的组件...
导出默认连接(mapStateToProps,actionCreators)(日历)