0

I have learned about how to use Redux as the state management library, but most of my project is using react useContext + useReducer since it's not a huge project.

And as far as I know, the Redux is similar to react useReducer which is only managing the state. But I want to create a dedicated file where I can put all the functions as useContext does.

Is it possible to use useContext + Redux? or is there any better solution for it?

Thanks in advance, appreciate any kinda response.

itsMe
  • 133
  • 12
  • Internally, React Redux uses React's "context" feature to make the Redux store accessible to deeply nested connected components. - [Docs](https://react-redux.js.org/using-react-redux/accessing-store) – PsyGik Jul 14 '21 at 09:24

1 Answers1

0

Neither useContext nor Redux tell you anything about how to structure your files. Theoretically you could put everything into one file or split it up into hundreds.

There are recommended project structures though.

Take a look at the Style Guide, which gives an example of a recommended file/folder structure.

Also, stay away from most external tutorials that still show switch..case reducers, connect or folder structures with separate action, reducer and constant files. Those are heavily outdated. Follow the official tutorial instead:

https://redux.js.org/tutorials/essentials/part-1-overview-concepts

phry
  • 35,762
  • 5
  • 67
  • 81
  • Is there something wrong with `switch`...`case`? (genuine question since you seem to suggest the outdated tutorials still follow them). The latest React docs seem to suggest (with a sidenote that both conditionals and switch are perfectly valid for use) using `switch`...`case` with `useReducer` instead of conditionals as represented in the Redux tutorial.https://beta.reactjs.org/learn/extracting-state-logic-into-a-reducer – Roast Biter Jul 16 '22 at 20:23
  • @RoastBiter `switch..case` implies that you write a reducer function by hand, which means you have to manually define action types and write the full logic in an immutable way. This is a lot of code and very error-prone. Since 2019, we have `createSlice` in the official Redux Toolkit that does all the "boilerplate" for you and allows you to write mutable logic in action creators. That saves tons of code and eliminates a whole class of bugs and complexity people have problems learning - and wouldn't need to learn at all. – phry Jul 17 '22 at 08:23
  • Please also see [Why Redux Toolkit is How To Use Redux Today](https://redux.js.org/introduction/why-rtk-is-redux-today) in the official Redux Documentation. – phry Jul 17 '22 at 08:24