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

javascript - How to get axios.post info after click a button once - Stack Overflow

programmeradmin2浏览0评论

When I want to get info from axios.post by clicking a button. But now I wanted to click the button twice to display the info. How should I do?

class ButtonComponent extends React.Component {
state = {display:"ini"};
rep = "ini";

click() {
    var self = this;
    axios.post("url", {})
        .then((rep) => {
            self.rep = rep.data;
        })
        .catch((err) => {
            self.rep = err;
        }) 
    this.setState({
        display: this.rep
    });         
}
render() {
   return  (
    <div>
        <button onClick={this.click.bind(this)}> click me </button>
        {this.state.display}
    </div>
   );
   } 
  };

When I want to get info from axios.post by clicking a button. But now I wanted to click the button twice to display the info. How should I do?

class ButtonComponent extends React.Component {
state = {display:"ini"};
rep = "ini";

click() {
    var self = this;
    axios.post("url", {})
        .then((rep) => {
            self.rep = rep.data;
        })
        .catch((err) => {
            self.rep = err;
        }) 
    this.setState({
        display: this.rep
    });         
}
render() {
   return  (
    <div>
        <button onClick={this.click.bind(this)}> click me </button>
        {this.state.display}
    </div>
   );
   } 
  };
Share Improve this question edited Mar 26, 2018 at 1:18 user8490655 asked Mar 23, 2018 at 9:08 user8490655user8490655 331 gold badge1 silver badge5 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

This will not display the values you need. Because at the time you are setting up the state inside click method your promise is not resolved.

You can do something like this. Once a user click on button disable button till the axios post is done. Then call setState so that you can see that data inside your div.

class ButtonComponent extends React.Component {

    constructor(props){
              super(props);
              this.state = {
                     data: '',
                     isLoading: false,
               };
               this.click = this.click.bind(this);
        }

    click() {

        this.setState({ isLoading: true });

        axios.post("<MY_URL>:3900/find/rapinfo", {})
            .then((response) => {
                  this.setState({ data: response.data, isLoading: false });
             })
            .catch((err) => {
                  this.setState({ data: err, isLoading: false });
             });
    }

    render() {
       return  (
            <div>
                <button onClick={this.click} disabled={this.state.isLoading}> click me </button>
                {this.state.data}
            </div>
           );
        }
    }

You've 2 options.

Bind your click event in the constructor:

constructor(){
  this.click.bind(this)
}

And then your button bees

<button onClick={this.click()}> click me </button>

or you can use es6 arrow functions to pass this too it, and declare your click functions like this:

click = () => {

}

and then your button will bee:

<button onClick={this.click}> click me </button>
发布评论

评论列表(0)

  1. 暂无评论