2

I've read this article (https://medium.com/@stowball/a-dummys-guide-to-redux-and-thunk-in-react-d8904a7005d3) a couple of times, but I'm still uncertain how to call your action inside of a component.

I've setup my actions and reducers correctly (I hope), so I'm at the last step now, which is using your action. I know that I first have to import my action and then use connect from redux to connect it to the store.

I currently have this in my component:

  componentDidMount() {
    this.serverRequest = axios
      .get('http://helloworld/customers')
      .then(res => {
        // Rerender state
        this.setState({
          res,
          dataToDisplay: res.data
        })
      })
  }

So my question is: How can I use my action inside of my component?

1 Answer 1

5

As anything else you have to pass the action creator to your component via props. You can do this by using the react-redux library and the mapDispatchToProps function.

You will want to "bind" your action creator first by wrapping it in another function that calls dispatch on your action creator.

I highly recommend reading the following redux docs as it explains it rather well and also provides examples.

https://redux.js.org/basics/usage-with-react#implementing-container-components

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.