Search code examples
reactjsreact-reduxreact-hooksreact-contextredux-observable

Is React Context API suitable for large scale applications


I am planning to build a large React application which might contain hundreds of components. But not sure what state management system to use between Redux and Context API.

Context API is in-built in React and doesn't need any third party library. It is easy to implement and solves the problem of sharing states at different levels of the component.

But on the other hand Redux is the Industry standard and has support for middleware to perform async actions.

If I choose Context API how can we manage API calls with it. Also do you think it is a good idea to use context for a large application where we might need state objects extensively.


Solution

  • The design benefit from Redux is that the action does not implement. An action is an indication that something happened (for example SAVE_PROFILE_CLICKED) but the action doesn't do anything (like connecting to api, sending data and saving response in state). You can do this with context api but the separation isn't enforced as much and you won't have the redux devtools. The pattern is called event store/sourcing. You could change the reducer and replay the events to see if your changes work and create a consistent state, testing is easier, extending is easier, logic is better isolated and probably many more benefits to be had.

    The design also separates writing to state (reducer), side effects (thunk) and reading from it (selectors). This pattern (writing/reading separation) is called cqrs. Your query/selector is separated from the command/reducer. This gives you easier testing, isolation of logic, less chance of duplicate implementation and probably many more benefits.

    You can still make a complete mess of your project when using Redux and not fully understand it so using Redux does not guarantee anything.

    If I choose Context API how can we manage API calls with it.

    You can do it any way you like it, the question is too general to answer.

    Also do you think it is a good idea to use context for a large application where we might need state objects extensively.

    As stated before; Redux is no guarantee your project won't be a mess. It will give you the tools to implement certain patterns with more ease. Make sure you understand it and it's patterns. Most example applications don't demonstrate why Redux is so powerful as the problem they implement (counter, todo app) isn't complex enough to even warrant using it. I can only advice you would write code that you're comfortable with and can understand.