Search code examples
reactjsreduxuse-context

Using both redux and useContext in your react app


Are there reasons why one would use both redux and useContext in their APP?

If so, what are they? Can you give examples of when to use either?


Solution

  • It is not suggested to use both redux and context api in single app.

    Both context and Redux has advantages while developing the application in their own way. Depending upon the use case you need to decide whether to go with context or redux.

    Context

    If the application is smaller and you need to have a centralized store that needs to store and access the datas. You can use context api. But the code will not be that organised when compared to redux.

    The below reference will provide you an outline on creating the context,

    https://reactjs.org/docs/context.html#reactcreatecontext

    The below will provide you the reference on using the context,

    https://reactjs.org/docs/hooks-reference.html#usecontext

    Redux

    If the application you are developing is bigger or enterprise level application. You can go with redux which provides more optimized code structure compared to context api.

    If you want to integrate and use redux. Use reduxtoolkit for simple and elegant usage.

    Use the below link for reference,

    https://redux-toolkit.js.org/introduction/getting-started