Search code examples
reactjsreduxstate-managementreact-hooks

React hooks: are they useful for shared state management, like e.g. Redux?


There is a hype about React hooks. Too much information and I still don't know: does the advent of hooks mean that libs like Redux can be thrown to garbage?

So far, what I understood is hooks are good for stateful functional components, what about shared state?


Solution

  • No, hooks don't totally eliminate the need for Redux. Hooks are mainly as an alternative to implement features that we have to use classes for today:

    1. Local component state
    2. Context
    3. Lifecycle methods and side effects

    Other than the above, hooks also provide an easier way to share stateful logic between components.

    What is more likely to kill/replace Redux is context instead of hooks, which is a way to share state across components. But IMO context isn't as powerful as Redux stores as there are other features that Redux offers besides a shared state store such as middlewares and a specialized devtool with time-travelling capabilites. There's also a whole learning and tooling ecosystem built around Redux that context doesn't have at the moment as far as I know.

    If you use the useReducer hook in conjunction with context like in this example, it'd be very similar to using Redux and for small apps (like a TodoMVC), it might be sufficient. For large apps I don't think just one context and useReducer will be sufficient. You might need multiple of them, and that's where using Redux and composing stores would make sense. You could also combine multiple contexts and useReducer hooks but it might be cleaner to just use Redux.