What is the right way to do asynchronous operations in Redux?

It’s understandable why so many newcomers to React+Redux have difficulty wrapping their heads around asynchronous actions. React is simply a view layer. Redux is simply a state management layer. But it takes much more to build a typical app.

These poor developers are given the corner pieces of a puzzle and the rest of the pieces are scattered about on the ground. There’s no reference picture to see what the puzzle is supposed to look like — everyone you ask would show you a different picture anyways.

Even developers experienced in React+Flux have difficulty picking up Redux because now they’re told not to put asynchronous calls in their components. Well then, where?

Continue reading “What is the right way to do asynchronous operations in Redux?”

What is the right way to do asynchronous operations in Redux?

4 Quick Tips for Managing Many Sagas in a React-Redux-Saga App

Writing code any other way, once you’ve embraced the Redux-Saga way of implementing asynchronous operations, is difficult. Everything starts looking like a saga. You add a feature, and another, and another… and, soon, your project has 50+ sagas in it. (I just counted — my current project has 52 sagas).

But have you ever added a saga and jumped over to your app only to find that your saga doesn’t seem to be running? What’s going on? webpack --watch is running, the bundle was updated…

One downside to having a boatload of sagas is that each saga has to be registered with the redux-saga middleware in order to run. But fifty-two lines of repetitive code? I don’t think there’s a JavaScripter alive that wouldn’t cry foul over all that ‘boilerplate’.

Here’s how I managed to manage all those sagas without too much repetition.

Continue reading “4 Quick Tips for Managing Many Sagas in a React-Redux-Saga App”

4 Quick Tips for Managing Many Sagas in a React-Redux-Saga App

Has Redux-Saga Got You Stuck In an Infinite Loop?

You might start out with a saga that looks something like the following code. No problem, it runs perfectly.

function* fetchSomethingSaga() {
    // Run this saga forever
    while (true) {
        // Wait for a fetch request
        const action = yield take(Action.FETCH_SOMETHING_REQUEST);

        // Extract the id of what we need to fetch
        const id = action.payload;

        try {
            // Make the API call
            const thing = yield call(apiClient.get, `/thing/${id}`);

            // We succeeded, return the thing
            yield put(fetchSomethingSuccess(thing));
        catch (err) {
            // We failed, report the error
            yield put(fetchSomethingFailure(err));
        }
    }
}

At some point, you decide to replace take with takeEvery so your saga can handle concurrent requests. Or you replace it with takeLatest so you can process the most recent request.

But… your events start firing in an infinite loop. Hey, what gives?

Continue reading “Has Redux-Saga Got You Stuck In an Infinite Loop?”

Has Redux-Saga Got You Stuck In an Infinite Loop?