// Pass a loading to the WithSpinner component // No idea why not just use in the WithSpinner component const mapStateToProps = createStructuredSelector({ isLoading: selectIsCollectionFetching, });
// right to left const CollectionOverviewContainer = compose( connect(mapStateToProps), WithSpinner )(CollectionOverview);
exportfunction* fetchCollectionsAsync() { yieldconsole.log("fetchCollectionsAsync in Saga is called");
try { const collectionRef = firestore.collection("collections"); // similar to async await
const snapshot = yield collectionRef.get();
// call a function that returns a promise const collectionsMap = yield call( convertCollectionsSnapshotToMap, snapshot );
// Put -> dispatch an action // if success, we dispatch fetchCollectionsSuccess, store the collectionsMap in the redux store yield put(fetchCollectionsSuccess(collectionsMap)); } catch (error) { yield put(fetchCollectionsFailure(error.message)); } }
// same name to fetchCollectionsStart in shop.actions.js exportfunction* fetchCollectionsStart() { yield takeEvery( ShopActionTypes.FETCH_COLLECTIONS_START, fetchCollectionsAsync ); }
In my opinion, redux-saga shines if you need to manage the interactions of long-running tasks. For example, I worked on software for set top boxes which needed to manage changing channels. There’s several asynchronous things that need to be done in sequence to start up playback, and then during playback extra things need to be done periodically. Additionally, every step needs to be cancellable in case the user changes channels. Something like that is easy to do with sagas (assuming you’re used to working with them), but comparatively hard with un-cancellable promises.
If you just need to do fairly simple stuff like “when they click a button, download some data”, then both of them do the job just fine. If your cases are fairly simple, i would lean towards redux-thunk because it’s learning curve is easier than redux-saga. Promises are common in the industry, and so many developers are already used to them; while sagas are a fairly specialized piece of code.
Don’t worry about difference in performance. In both cases, there’s only a very small amount of time spent running the library’s code. The bulk of the time will be spent running your code, and while the style of your code will be quite a bit different, the number of things you need to calculate to do a certain job will be very similar.