Razzle: 构造函数 - 调用 super 和 bind

创建于 2016-06-09  ·  3评论  ·  资料来源: jaredpalmer/razzle

@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”。

问题,我将如何在您的入门项目中使用它?

最有用的评论

好像您没有使用connect react-redux

从'react-redux'导入{连接}

...您的组件...

导出默认连接(mapStateToProps,actionCreators)(日历)

所有3条评论

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 的课程:

另请查看connectreact-redux API: https ://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops -mapdispatchtoprops-mergeprops-options

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

dizzyn picture dizzyn  ·  3评论

gabimor picture gabimor  ·  3评论

knipferrc picture knipferrc  ·  5评论

alexjoyner picture alexjoyner  ·  3评论

GouthamKD picture GouthamKD  ·  3评论