Home > other >  Where should I put API call which is requesting user data
Where should I put API call which is requesting user data

Time:06-17

Where should I put get user data API call which is requesting user data such as username, email from JWT token stored in localstorage. Should I call it from _app.js pass it to the components or should I create redux store and save these data.

CodePudding user response:

Using redux only for that purpose is an overkill. You should create context and wrap components, which are using this data. Also there is hook called useReducer, combined with context API allows you to achieve redux behavior.

CodePudding user response:

Passing user data down to components directly is not a bad idea if you have only 2 level depth structure which is often not the case.

For this purposes is a nice option to use tools like Redux or React Context API. That way you can access the global state from whichever component you like in the same way, which leads to more readable and maintainable code. For more information about Redux vs Context API, consider looking more in-depth to understand the differences and decide which one is more suitable for your case.

For me personally, the Context API can do the work in more of the cases which is fine. Also, it is already part of React, and it's not a dependency like Redux is.

  • Related