Search code examples
react-nativereduxreact-reduxredux-thunk

Use redux if all the data is in ajax requests or database


I am new to react, specifically in native and I am in the dilemma if I use redux to maintain the data store if most of these will be in local database. At the moment I have thought about having a "categories" with a single case that would be of the SET_DATA type and every time I need to filter the data I call asynchronous actions methods that load the necessary data and perform the dispatch for the reducer. An example of my code is like this (where ALL_CATEGORY and BY_NAME_CATEGORY are queries):

categoryAction:

import {SET_CATEGORIES} from "../constants/actionTypes";
import {ALL_CATEGORY, BY_NAME_CATEGORY} from "../constants/db/load";
import {dbTransaction} from '../database/dbbase';

export const setCategories = (data) => {
    return {
        type: SET_CATEGORIES,
        payload: data
    };
};

export const allCaregories = () => {
    return (dispatch) => {
        dbTransaction(ALL_CATEGORY)
            .then((data)=> {
                dispatch(setCategories(data));
            }).catch((error)=> console.log('ALL_CATEGORY ERROR'));
    };
};

export const byNameCaregories = () => {
    return (dispatch) => {
        dbTransaction(BY_NAME_CATEGORY)
            .then((data)=> {
                dispatch(setCategories(data));
            }).catch((error)=> console.log('BY_NAME_CATEGORY_CATEGORY ERROR'));
    };
};

And de categoryReducer is:

import {SET_CATEGORIES} from "../constants/actionTypes";

const initialState = [];

const categoryReducer = (state = initialState, action) => {
    switch (action.type){
        case SET_CATEGORIES:{
            return action.payload;
        }
        default:
            return state;
    }
};

export default categoryReducer;

This works, but my query is why not choose to create methods to directly call the local database without using redux? Is there any advantage using redux or is it just to separate the application in layers?

Same case if the data were 100% in a web service, what would be the advantage of using redux if the data will always be obtained from an external source to redux


Solution

  • why not choose to create methods to directly call the local database without using redux?

    You can definitely choose to not use redux for your application; React/React Native or any other framework has no dependency on Redux. Redux isn't anything special, it's actually only a couple dozen lines of code to organize the way your application deals with state. In my honest but biased opinion, I would use Redux if I think an application might need to scale beyond 1,000+ (very arbitrary number) users or have more than 1 developer.

    Is there any advantage using redux or is it just to separate the application in layers?

    There is no advantage to using Redux beyond maintainability and standardization. Your application might seem simple now only having to query a local database, you can definitely use custom methods to update the store. But, what happens when you need to make api requests too? Or when you need to add undo functionality? Will your code be comprehensible to new teammates? Ultimately, by using redux your chances of incurring technical debt are reduced.

    Same case if the data were 100% in a web service, what would be the advantage of using redux if the data will always be obtained from an external source to redux

    It will be no different than querying a local database. Redux doesn't care where you get data from it only handles where the Store is, when it should change, and how it should change.