Code Monkey home page Code Monkey logo

Comments (4)

tbtommyb avatar tbtommyb commented on June 8, 2024 3

Redux's simplicity kind of falls apart with async actions like API calls, IMO.

@pankajpatel as Sohil said redux-thunk is often used to handle API calls. You can see an example of how I used it here (at the bottom). I may not have done things correctly but it does work...

Triggering redirects from reducers would be an anti-pattern as they are meant to be pure functions. What I ended up doing was using redux-thunk to have a kind of 'master' action that makes the API calls and dispatches actions as it starts the request, receives a response, receives an error etc etc.

from learn-redux.

pankajpatel avatar pankajpatel commented on June 8, 2024 1

For an update:

I came across an interesting article which covers the API calls in the Middleware
http://www.sohamkamani.com/blog/2016/06/05/redux-apis/

And this solved my problem.

Though I am still wondering, how to trigger redirects from the Redux reducers or the middleware.

Any suggestions?

from learn-redux.

sohilpandya avatar sohilpandya commented on June 8, 2024 1

@pankajpatel have you had a chance to look at redux-thunk might be something worth investigating, can be of help when making API calls.

Apologies if you've already looked at it, just thought it might be something that may be of help to you. 😄

from learn-redux.

pankajpatel avatar pankajpatel commented on June 8, 2024

@sohilpandya thanks for suggesting redux-think but redux-think already in the app as the react app has Apollo GraphQL tools and is registered through that.

So I tried adding normal reducers which return functions instead of Data with dispatch and getState as parameters; as suggested in the Redux's documentation. But as a matter of hard luck, couldn't figure out the reason of not firing up the AJAX request and subsequent actions with SUCCESS/FAILURE.

@tbtommyb The approach looks really good, thanks for suggesting. Though in the article http://www.sohamkamani.com/blog/2016/06/05/redux-apis/, it is suggested to create a middleware and I feel that the middleware approach suits best to app configuration which I am using.
As I can quickly enable/disable or separate from app as a different package; similar to the auth plugin for hapijs at backend

My primary motive is to have as flexible as possible plug-ability of app modules because app will expect to have new features in the near future so setting up core like that becomes mandatory. The app is a rewrite of existing monolith and became very hard to keep up with new requirements of feature modules.

from learn-redux.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.