I am excited about new hooks API, but how to handle complex async logic?
Redux-Saga would be a perfect solution if there was any way to use Saga without Redux.
We are using redux-saga in our current project. It behave as a side-effect and you can configure a callback when an action is dispatched. The callback then do async work from sagas itself. Once there is a response/failure from the service promise, you can dispatch another action which will update the states.
Related
I am making an API call, after which I want to update the store with {apiExecuted: true} by calling the ACTION_API_EXECUTED action.
What is the best way to achieve this by using dispatch?
You need to do this asynchronously in the action with the use of thunk, saga or an epic, depending upon the configuration of your project.
Based on the API response, you can then dispatch another action call to update the state.
I am just learning how to implement redux into my react projects and I am trying to understand the advantage of running asynchronous code in middleware for redux state changes. Why wouldn't we just run the asynchronous code at the component level and then dispatch different actions on the completion of the asynchronous code at the component level?
Sometimes depends on your situation u have to call actions at same time u need middlewars for async actions and its clearer and simpler and more sync to call your apis in your actions.instead of fetch it in your component then pass it to your action.
Search many forum but still confuse, redux is async or sync??
Realize this is a basic question but I had no luck finding the answer elsewhere.
if any one know describe with examples.
Redux store only supports synchronous data flow (ref).
This is what you get by default with createStore().
Asynchronous middleware like redux-thunk or redux-promise wraps the store's dispatch() method and allows you to dispatch something
other than actions, for example, functions or Promises. Any middleware
you use can then interpret anything you dispatch, and in turn, can
pass actions to the next middleware in the chain.
For example,
a Promise middleware can intercept Promises and dispatch a pair of
begin/end actions asynchronously in response to each Promise.
Read More>>
Dispatching actions is synchronous in Redux.
Without middleware, Redux store only supports synchronous data flow. This is what you get by default with createStore().
For asynchronous actions, you can use applyMiddleware(), for middlewares such as redux-thunk.
I was working on creating the react-redux application. But stuck on the point why we shouldn't call the async function (for API calling) in the component method like componentWillMount etc? Why I've to use the middleware like redux-thunk & saga? Why we cannot call the API request from the action and based on that result, we call the reducer? Is it the bad idea? I didn't understand deeply about this. Can you please help me out to understand the concept of API calling in redux?
As far as I see it, you can call async functions anywhere, including in componentWillMount. What a middleware like redux-thunk does, is to add IDs in the dispatched actions, 'ordering' them, avoiding race conditions such as the one that is described in Writing Async Code in redux
Building a React application with redux as the flux pattern.
To use an api a token exchange request needs to happen. This has to occur only once when the React application starts. The access_token returned from the token exchange needs to be stored in the redux state and accessible by other api calls.
One approach i tried is by calling a redux action in the App(parent) component, and then all other calls are within children components. New to React i now understand that this is not the right approach as the React components lifecycle events and the actions and reducers called do not follow a synchronous flow, so the redux actions are called asynchronously.
How do i ensure a redux action is called prior to any other redux action that depends on the results of the first redux action? Which middleware would be best suited for ensuring a synchronous approach redux-thunk, redux-look and redux-saga.
If you need a process to run before any other action, you could do it before creating the store, and passing the result in your main reducer function at store creation.
Then, just render the app after the promise or callback of this process is done.
Otherwise, just dispatch this initial action after the store creation, and before app render (with redux-thunk if it is async).
You can use redux-thunk to have a "promise like" behavior when displatching actions. This way you can ensure an action was done before dispatching another one.