M
M
Mikhail Yarema2017-10-13 12:22:17
JavaScript
Mikhail Yarema, 2017-10-13 12:22:17

How to pass data from promise(axios) to state redux?

componentDidMount(){

     axios.post('/', {})
      .then(function (response) {
        this.props.allDocuments(response.data);
      })

      .catch(function (error) {
        console.log(error);
      });
      
    }

This approach gives me an error:
TypeError: Cannot read property 'props' of undefined
at App.js:15
at
How to pass the context correctly?
Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2017-10-13
@frezebloom

axios.post('/', {})
  .then((response) => {
    this.props.allDocuments(response.data);
  })
  .catch(function (error) {
    console.log(error);
  });

Or use bind.

M
Maxim, 2017-10-13
@maxfarseer

Specifically in your example, you can just keep a reference to this or use any other options for binding / context forwarding ...

const that = this
that.props...

But this is bad(!!!) advice. Since most likely you are doing something "not according to the textbook." In componentDidMount you should have an action creator call, inside of which axios will be called and from then from which you will dispatch the event.
ps don't forget about redux-thunk included, otherwise you will have to import dispatch into your actions, which will also be out of the tutorial ;)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question