- July 17, 2018
- Posted by: AFourTech
- Category: Blogs
Being a React developer is a common thing and facing problems regarding Angular stack in your project is not so common.
But, changing base framework approach would surely provide overall knowledge of how to structure and develop apps. Eventually, it would lead to adopting some of the Angular solutions.
So, let’s have a look at how Angular can help become a better React developer:
It becomes a cumbersome task when it comes to naming project directories like “components/containers/styles” or in redux — “reducers/actions/types”.
In this case each one of the folders contained components, but usually, in component-based frameworks, people do grouping by file type.
If we consider React native, then this app is not like that, here it is more common to group component files in their folders.
But, here some crucial questions arise! How to scale-up when there are hundreds of components, maybe thousands? How to structure domain model and view containers related to arbitrary components?
Modules are the perfect answer to this problem!
However, let’s have a look at what Angular modules do:
- Group domain functionality
- Divide app for smaller pieces that can be plugged on/off
- Group lazy loading scope
- Declare module-specific components, pipes, and directives
- Export public components, pipes, and directives
- Import other modules to have access to exported declarations and re-export imports
- Provide services for Dependency Injection
- Example of modules in Angular could be “CheckoutModule” in e-commerce shop or “InputsModule” containing custom input components used in forms.
These are some of the great functionalities that Angular provides, but let’s shed some light on how this can help in React projects?
- Creating modules directory in src, which is the base parent for domain pieces.
- Each module contains components specific to its scope.
- Each module contains its data model, actions, effects, reducers, services.
- Additionally, each module could contain index.js file, reexporting all local files, so they can be accessed importing by other modules.
The main benefit is, it scales. It helps the developer to avoid getting lost under thousands of components without any context.
2: Dependency Injection
Angular DI is great concept, though it’s not that popular in frontend development.
Angular DI is great and different, both at the same time because in this concept each component or service can receive the instance of any service created during bootstrapping the app.
In this, the services provided in modules are globally constructed singletons, but there is a hurdle. As, their scope is restricted to components, services in Angular can’t be scoped to a module.
Despite some minute disadvantages, there is a great benefit of having DI Container. It goes an extra mile by decoupling all dependencies and bringing it to one layer.
Just register some class (or value) to injectionToken, so later accessing this token will resolve proper class.
In this way, one can bring app configurations, services to components or replace mock data services instead of data providers until they implement the same public interface.
But, some may still ponder, how do this benefit in React?
This would surely help, but a developer should write better, more testable code, by keeping more SRP and manually injecting smaller, more specific services to another.
Also, keeping on experimenting with libraries like Bottle.js would surely fetch similar effect as in Angular.
Another best feature of Angular is Typescript.
Angular is mostly sought after because many developers can easily work on the project at the same time and non-java script developers will also understand the code.
This is the reason it is being widely used in corporations and huge projects.
Both reasons are caused not by the framework, but by Typescript.
Libraries with typings are self-explaining and developer doesn’t have to check docs all the time. Also, the code style is easy to understand by Java, C# developers.
By applying the above-mentioned techniques a React developer can be a better developer with Angular.