最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Binding this to nested functions in React - Stack Overflow

programmeradmin5浏览0评论

How can I bind this to the React ponent when dealing with a nested function?

Here is a skeleton example. The reason that function2 is nested is so that you can get access to the variables defined in function1

class View extends Component{

    constructor(props){
        this.function1 = this.function1.bind(this);

        this.state = {
            a = null;
        }
    }

    ponentDidMount(){
        function1();
    }

    function1(){
        console.log(this); //will show the ponent correctly

        var param1 = 10;

        //call a second function that's defined inside the first function
        function2();

        function function2(){
            console.log(this); //undefined

            var a = param1*2;

            this.setState({ a : a}); //won't work because this is undefined
        }
    }

    render(){
        return(
            <div>
                <p> Hello </p>
            </div>
        )
    }
}

How can I bind this to the React ponent when dealing with a nested function?

Here is a skeleton example. The reason that function2 is nested is so that you can get access to the variables defined in function1

class View extends Component{

    constructor(props){
        this.function1 = this.function1.bind(this);

        this.state = {
            a = null;
        }
    }

    ponentDidMount(){
        function1();
    }

    function1(){
        console.log(this); //will show the ponent correctly

        var param1 = 10;

        //call a second function that's defined inside the first function
        function2();

        function function2(){
            console.log(this); //undefined

            var a = param1*2;

            this.setState({ a : a}); //won't work because this is undefined
        }
    }

    render(){
        return(
            <div>
                <p> Hello </p>
            </div>
        )
    }
}
Share Improve this question edited Feb 8, 2018 at 3:07 Badrush asked Feb 8, 2018 at 2:41 BadrushBadrush 1,2871 gold badge17 silver badges36 bronze badges 3
  • Why do you need the nested function2? It's only accessible within function1 and you're not calling it. – Sam R. Commented Feb 8, 2018 at 2:49
  • 1 use arrow function for function2 is one option, const function2 = () => {...} - though, you never use function2, so there's no issue – Jaromanda X Commented Feb 8, 2018 at 2:49
  • I edited my post to call function2() from within function1(). The goal is to also have function3() and function4() that can all use the same initial variables defined in function1() – Badrush Commented Feb 8, 2018 at 3:08
Add a ment  | 

2 Answers 2

Reset to default 12

Why not just use arrow functions? That would make sure this is referenced properly. I am assuming you can use ES6.

function1 = () => {
    console.log(this);

    var param1 = 10;

    const function2 = () => {
      console.log(this);

      var a = param1*2;

      this.setState({ a }); 
    }

    function2(); // inkove the function
}

OR if you only want to use ES5, then this could also work

function1() {
    console.log(this);

    var param1 = 10;

    function function2() {
      console.log(this);

      var a = param1*2;

      this.setState({ a }); 
    }

    function2.bind(this)() // to invoke the function
}

You could keep a reference to this and use that variable inside function2.

function1(){
    var self = this;
    .....
    function function2(){
        console.log(self);
        ....
    } 
}

You can also set the context of that function using apply, bind or call. For example:

function2.call(this);
发布评论

评论列表(0)

  1. 暂无评论