简体   繁体   English

用react-redux绑定事件处理程序prop

[英]Binding event handler prop with react-redux

What is the proper way to declare event handler from container to the presentation widget so that I can access other props in the event handler function? 从容器到演示文稿小部件声明事件处理程序的正确方法是什么,以便我可以访问事件处理程序功能中的其他道具?

class ApplicationWidget extends Component {
    componentDidMount() {
        this.props.handle_onload.call(this);
    }

    render() {
        return (
            <div onClick={this.props.handle_click}>
                <Header />
                <Content />
            </div>
        );
    }
}

export default connect(
    state => {
        return {foo: state.foo};
    },
    dispatch => {
        return {
            handle_click() {
                console.log(this)
            },
            handle_onload() {
                jQuery.get({
                    accepts: 'application/json',
                    success: (data) => dispatch(the_action_creator(data)),
                    url: `/initialize`
                });
            }
        };
    }
)(ApplicationWidget);

Currently the this.props.handle_click event handler logs undefined whenever the click event happens. 当前,每当单击事件发生时, this.props.handle_click事件处理程序都会记录undefined的日志。 If I want access to this.props.foo , what is the right way of doing it? 如果我想访问this.props.foo ,正确的方法是什么? My current implementation is 我当前的实现是

<div onClick={this.props.handle_click.bind(this)}>

in the render() method and it works as intended however according to the linter this doesn't look like a good practice. render()方法中,它可以按预期工作,但是根据lint,这似乎不是一个好习惯。 The following code doesn't seem to work after the container (generated by the connect function) is updated (the binding is reseted to undefined for some reason) 在更新容器(由connect函数生成)后(由于某种原因绑定重置为undefined ),以下代码似乎不起作用

constructor(props) {
    super(props);
    this.props.handle_click = this.props.handle_click.bind(this)
}

So what is the right way to do this? 那么正确的方法是什么呢? Or am I doing this whole thing wrong? 还是我整个事情做错了?

The prop handle_click is just a function that is passed to the component by reference, so it doesn't “know” anything about the scope ( this ) of the component. prop handle_click只是一个通过引用传递给组件的函数,因此它不“了解”有关组件范围( this )的任何信息。 You can change that using the bind method that is available to all functions, like this: 您可以使用所有功能都可用的bind方法来更改它,如下所示:

class ApplicationWidget extends Component {
    componentDidMount() {
        this.props.handle_onload.call(this);
    }

    render() {
        return (
            <div onClick={this.props.handle_click.bind(this)}>
                <Header />
                <Content />
            </div>
        );
    }
}

To optimize this and prevent your linter from complaining, you can bind it in the constructor like this: 为了优化这一点并防止您的lint抱怨,可以将其绑定到构造函数中,如下所示:

class ApplicationWidget extends Component {
    constructor(props) {
        super(props);
        this.handle_click = props.handle_click.bind(this);
    }

    componentDidMount() {
        this.props.handle_onload.call(this);
    }

    render() {
        return (
            <div onClick={this.handle_click}>
                <Header />
                <Content />
            </div>
        );
    }
}

So you almost got it right, but I wouldn't modify the props in the constructor, just add another method to the class. 因此,您几乎完全正确,但我不会在构造函数中修改props,只需在类中添加另一个方法即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM