The whole world is moving to the eCommerce store and selling products online. And nowadays, People are moving to Shopify for eCommerce online store ...
React design patterns and structures of Redux and Flux
Every developer has a question arrives in mind during starting of the projects about design patterns and structures like which are the most suitable patterns for projects. Different projects and companies follow different patterns based on the needs and preferences of their developers. This means it’s very likely you’ll see React + Redux projects structured and Flux architectural patterns.
The architectural pattern is a way of organizing the parts of your software and a way to pass information between them while redux is a library. An implementation of a pattern is conceptually quite similar to Flux.
Table of Content
- Redux Suggested Code Structures
- Organized by feature
- Domain-style Structures
- Ducks-style Structures
- What is the Flux?
- Flux vs. MVC
- Flux vs. Redux
Redux Suggested Code Structures
Redux is just a library, there are a few common patterns that most Redux developers tend to use:
Organized by feature: separate folders for “actions”, “constants”, “reducers”, “containers”, and “components” .
Domain-style: separate folders per feature or domain, possibly with sub-folders per file type.
Ducks-style: similar to domain style, but explicitly trying together actions and reducers, often by defining them in the same file.
Organized by feature Structure
One of the most basic, standard source code file structures for small React + Redux projects looks like this:
The benefit of either variation of the structure above is that it’s rather straightforward, making it great for smaller React + Redux applications. There is no denying in the fact that it may seem reasonable to group similar objects together like controllers with controllers, components with components. But what’s more interesting is imagining a large React application with 100 or above components? Just sticking them all in that components sub directory would get unorganized.
Let’s summarize all the issues we have with this structure:
- Components folder becomes bigger and it’s hard to understand which components are reusable – without this visibility, you can easily break things up by changing the component which is (unexpectedly!) used somewhere else in your app
- High coupling between modules featuring low cohesion from within. As soon as a new component is introduced. You will start requiring all the files used to make it work. However, the new component reducer does not care about dependent reducer at all)
- Modularizing your app is no longer easy – Easily pulling things apart from the part of your code and replacing it with something new is never easy.
So it is much better than files are grouped not by nature, but by the business domain. In a large project, it is often recommended to organize by feature as this provides you the ability to focus on the feature at hand And you no longer have to worry about navigating the entire project. For example, if I need to change something related to todos, working solely within that module becomes quire easy and you no longer even have to think about the rest of the application. In a sense, In a domain-style structure, content is organized by domain or “area” of the application.
Below is the suggested domain style example below, we see the app, item, and user sub directories. Each of these corresponds to a different page, area, and/or domain of the application.
Notice that the container components, reducers, action creator files, and all other presentational components are all together in the sub directory corresponding to the domain they’re responsible for, instead of sticking all reducers in a single folder or all actions in a single directory.
This allows developers to more easily access all their necessary files in one place when working on a specific area of a React + Redux application. Instead of digging the action creator out of actions, and trying to locate all its necessary components in one big component directory, everything necessary for the specific module area is in one file. In this structure, Please note that tests merit moving from the committed catalog (for example __tests__ or whatever) to the areas. We as React developers want to give the test document a similar name as the record it tests, however, including – test toward the end, for example in the event that we have to test item.reducers.js the test document will be called item.reducers-test.js.
When a file structure is created which is scalable and easy to follow it is known as ducks. Besides, it is basically a proposition for packaging reducers, activity types, and activities all into a similar document. It leads to smaller, more manageable project directories, and developers have to alter and navigate between few files instead of changing multiple files.
Several rules should be followed in creating and using these modules. A duck folder MUST:
- Contain the entire logic for handling only ONE concept in your app, ex: product, cart, session, etc
- Have an index.js file that exports according to the original duck rules
- Keep code with a similar purpose in the same file, such as reducers, selectors, and actions
- Contain the tests related to the duck
Keep in mind, Ducks is not a set framework, it’s an idea about how to organize your files and as such can be customized for your specific use case
The Code Structure section of the Redux documentation FAQ also contains a great discussion about structural decisions, and further links and resources.
Now let’s move with another popular react architecture for React application development.
What is the Flux?
At the point when an application architecture or pattern is designed, developed, and utilized by Facebook to assemble UI or customer side web applications, it is known as Flux. Flux Architecture: In a framework, it is a general design pattern that implements a single mediator (either a reducer or store, depending on implementation) for all actions, through which the application state is processed and returned to the view. The Flux design pattern is made up of four parts, organized as a one-way data pipeline.
Flux is probably better explained by explaining its components:
Actions – Helper methods that facilitate passing data to the Dispatcher.
Dispatcher – Receives actions and broadcasts payloads to registered callbacks.
Stores – Containers for application state & logic that have callbacks registered to the dispatcher.
Controller Views – React Components that grab the state from Stores and pass it down via props to child components.
Flux vs. MVC
Flux has been presented as an alternative to MVC. Now what was the main issue with MVC, you may ask? Well, it doesn’t scale well for the very huge codebase. If simply put, MVC works like the view to get information from the model and display it to the user. The user then interacts with it. In the end, these interactions trigger the view to update the data stored in the model, which then may trigger a UI update in the view. In an MVC each of these models will fire its view update which makes it hard to debug (chains of changes vs 1 change) and can cause parts of your app to fall out of sync in some edge cases.
How about we investigate the fundamental factors because of which Flux has an advantage over MVC configuration design.
- Flux is very exacting about the progression of the application. The information Dispatcher advances some exacting standards and special cases to oversee the stream. There is no such thing in MVC, and the flows are implemented differently.
- Flux follows Unidirectional Flow, all the changes go in the same direction through the data Dispatcher. The Store cannot change by itself, this is the same concept for all other Actions. Changes to be made need to experience the Dispatcher through Actions.
- While MVC can’t demonstrate single items, Flux can do it to store any application-related information.
Flux vs. Redux
Flux comes with the best practices that suggest a specific implementation of the architecture. Facebook, thet original creator of Flux architecture, wrote a library called Flux to help implement this architecture in applications. However, Dan Abramov felt that this architecture could be simpler. Dan Abramov and Andrew Clark built a Flux-inspired library that was widely considered better than Facebook’s Flux library it knows as Redux.
REDUX = REDUCERS + FLUX
Redux architecture introduces new concepts like
- Reducer: Logic that decides how your data changes exist in pure functions.
- Centralized store: Holds a state object that denotes the state of the entire app.
- Flux includes multiple Stores per app, but Redux includes a single Store per app.
- Flux store handles all business logic, while redux Reducer handles all business logic.
Overall, these powerful design patterns make your apps UI attractive and usable and it is equally important for developers to know their usages. It totally relies upon the advancements you embrace, the point of your application just as the worldview you like to utilize. In case you would like to know more about Flux and Redux, you can contact Web App Development Company like us.