Question:
What is the most convenient and recommended best practice for organizing containers, components, actions, and gears in a large React / Redux application?
My opinion:
Existing trends seem to organize reductant gates (actions, gears, sagas ...) around a related container component. eg.
/src /components /... /contianers /BookList actions.js constants.js reducer.js selectors.js sagas.js index.js /BookSingle actions.js constants.js reducer.js selectors.js sagas.js index.js app.js routes.js
It works great! Although it seems there are a couple of problems with this design.
Problems:
When we need to access actions , selectors or sagas from another container, it seems like an anti-pattern. Let's say we have a global container /App with a reducer / state that stores information that we use throughout the application, such as categories and enumerations. Following the above example with a state tree:
{ app: { taxonomies: { genres: [genre, genre, genre], year: [year, year, year], subject: [subject,subject,subject], } } books: { entities: { books: [book, book, book, book], chapters: [chapter, chapter, chapter], authors: [author,author,author], } }, book: { entities: { book: book, chapters: [chapter, chapter, chapter], author: author, } }, }
If we want to use the selector from the /App container in our /BookList container, we need to either recreate it in /BookList/selectors.js (certainly wrong?) OR import it from /App/selectors (will always be EXACT same selector ..? no.). Both of these ratings seem to me suboptimal.
A striking example of this use case is authentication (ah ... auth, we love to hate you), as this is a VERY common side effect model. We often have to access /Auth sagas, actions and selectors throughout the application. We can have containers /PasswordRecover , /PasswordReset , /Login , /Signup .... Actually, in our application, our /Auth contianer does not have any valid component at all!
/src /contianers /Auth actions.js constants.js reducer.js selectors.js sagas.js
Just containing all the Redux backups for the various and often unrelated auth containers mentioned above.