Comments (4)
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.
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.
@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.
@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)
- Add these articles to repo? HOT 4
- npm ERR! Failed at the [email protected] start script 'node devServer.js'. HOT 22
- Getting issue during deployment of nodejs file locally using heroku toolbelt HOT 3
- Enlightenment on Higher-Order Functions (components) HOT 2
- Add CONTRIBUTING.md File to Repo
- Please Correct link HOT 1
- Link: Build Yourself A Redux HOT 2
- Add links to Learn-Elm and Learn-Elm-Architecture Tutorials
- Fixing markdown HOT 1
- 'addTodo' and 'setVisibilityFilter' actions are unused in index.html example
- How long do the Egghead.io Redux Videos Take (Total Elapsed Time)? HOT 4
- Redux went from having 0 (Zero) Dependencies to having 4 (Four) ...
- Update/Improve instructions for how to use the Egghead.io notes/transcriptions
- Markdown Fail/Typo in section 29. HOT 1
- update links
- update github project link
- broken link Introductory Video Tutorials
- update https://github.com/dekujs/deku link
- [email protected] dev
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from learn-redux.