6
Aaronz
7y

ALL REACT-REDUX DEVS PLEASE UNITE!

I need help..

Comments
  • 3
    Whats up?
  • 3
  • 2
    So I've got to display an API response onto a modal, and when I use axios.get().then within a function in the component it seems to work fine. I get the response set it to a state and my modal displays it from the set state and everything's cool.

    But when I use actions and reducers to do the same the modal displays the empty state (i.e before the state is set, meaning that it sets the state even before getting the response from the action ). The state gets set after the modal populates and the second time I toggle the modal it displays the first API response.
  • 2
    Seems to me that your reducer returns something before the actual promise is handled. Check out https://github.com/pburtchaell/.... I don’t know how tight your schedule is, but i would recommend using Sagas for async operation. They have helped me keeping control over complex async actions troughout my apps.
  • 1
    Or Thunks. If you have the time, you can check out both and see which you prefer. I've used both at least once before; thunks are quicker to get working, while Sagas take just a tad more work but are therefore a bit more structured and clear, in my opinion

    @JohnDoesNot do you agree?
  • 0
    Thanks a ton guys @JohnDoesNot and @Allenll.

    Looks like I'll have to use saga because my boss just reviewed the issue and he suggested the same.

    I've just started working with Redux, guess I'll first have to get used to it before I start using saga.

    Also, do you guys have any good sources that you would suggest for me to learn sagas?

    Thanks :)
  • 0
    @Aaronz
    https://redux-saga.js.org/docs/...

    That's the official one. I think that's what I used to get a basic understanding; i say think because that was months ago and I don't remember at all how I figured it all out. 😅😅😅
  • 0
    I agree @AllenII, Thunk is indeed also a very good option. I like Sagas, but it can be a little overkill on small projects, in those cases Thunk is a good solution. The learning curve is pretty hard, so learning Redux and Sagas at the same time can be confusing :)
Add Comment