ReactJS - Need to click twice to set State and run function

前端 未结 3 1643
长发绾君心
长发绾君心 2021-01-02 06:40

Here is a summary of the code I have inside my React component:

getInitialState: function(){
  return{link:\"\"}
},
onClick1: function(){
   this.setState({         


        
3条回答
  •  半阙折子戏
    2021-01-02 07:13

    Well, here I am answering my own question, for future reference. I figured it out. I removed this.otherFunction() from the onClick functions, and put it in componentWillUpdate. So it looks like this:

    getInitialState: function(){
      return{link:""}
    },
    onClick1: function(){
       this.setState({link:"Link1"});
    },
    onClick2: function(){
       this.setState({link:"Link2"});
    },
    otherFunction:function(){
         //API call to this.state.link
    },
    componentWillUpdate(){
        this.otherFunction();
    },
    render: function(){
      return 
    Link1 Link2 //...some code to display the results of API call
    }

提交回复
热议问题