firebase :: Cannot read property 'props' of null

后端 未结 1 549
慢半拍i
慢半拍i 2021-01-25 22:36

Hello i am trying to use react-router with firebase. but it gives me this error.

Cannot read property \'props\' of null

this is exac

相关标签:
1条回答
  • 2021-01-25 23:05

    In your callback, the this pointer no longer points to your React component. There are many ways to deal with this:

    Capture this into another variable

    var component = this;
    query.on("child_added", function hello (snapshot) {
        ...
        component.props.router.push('Index');
    });
    

    Explicitly bind this

    query.on("child_added", function hello (snapshot) {
        ...
        this.props.router.push('Index');
    }).bind(this);
    

    Use fat arrows, which implicitly keeps this

    query.on("child_added", snapshot => {
        ...
        this.props.router.push('Index');
    });
    

    Note that this is a very common problem that developers have in JavaScript and I highly recommend you learn more about the way this behaves with callbacks:

    • Polymer this.push not working
    • how to call a function inside a self-executing anonymous function?
    • Use of the JavaScript 'bind' method
    • Firebase Login with React Components
    0 讨论(0)
提交回复
热议问题