0

In EMACSsCRIPT 6 code with React, if we use

var raxios = axios.get(some_server_link).then(function()(response) {
        .....
        use    response   data
        ....

});

I notice that I can only use response inside the function. Is there any way to be able to use response's info out of this function scope?

Jose Cabrera Zuniga
  • 2,348
  • 3
  • 31
  • 56

1 Answers1

1

Have you tried setting the response as state?

axios.get('/url')
  .then(res => {
  this.setState({ someKey: res.data });
}

Then you should be able to use it outside using:

this.state.someKey;
Jimmy Höglund
  • 173
  • 1
  • 10
  • No, I did not. I am trying to use react-bootstrap-table and to implement your answer I might have to extend it, some how. thx – Jose Cabrera Zuniga Apr 18 '17 at 16:32
  • 2
    he is using `es6`, this way it will throw the error `can't read setState of undefined`, you need to use `arrow function` inside `.then` or use `.bind(this)` to `bind` the `context` :) – Mayank Shukla Apr 18 '17 at 16:38
  • 1
    @JoseCabreraZuniga no problem if you are using `react-bootstrap-table`, store the data in state variable and assign that variable to `data` field of table, it will work :) – Mayank Shukla Apr 18 '17 at 16:40