Use cases of javaScript decorators From Scratch

Use cases of javaScript decorators From Scratch

The use cases of decorators are very boundless really. There are a few examples where decorators are utilized in certifiable applications.

Decorators in Angular

On the off chance that anybody knows about typescript and Angular, they would have certainly gone over decorators being utilized inside Angular classes. You can discover decorators, for example, "@Component", "@NgModule", "@Injectable", "@Pipe" and that's just the beginning. These decorators come stacked underlying and design the Class.

MobX

MobX vigorously used and empowered decorators before version 6. The decorators utilized were "@observable", "@computed" and "@action". In any case, MobX at present doesn't support the use of decorators as this proposition has not been normalized at this point. The documentation states as follows,

Be that as it may, decorators are at present not an ES standard, and the cycle of normalization is consuming most of the day. It likewise seems as though the standard will be not the same as the manner in which decorators were executed already.

Core Decorators JS

This JavaScript library gives readymade decorators out of the case. Albeit this library depends on the stage 0 decorator proposition, the creator of the library is delaying until the proposition arrives at stage 3, to refresh the library.

This library comes with decorators such as “@readonly”, “@time”, “@deprecate” and more. You can check out more over here.

Redux Library in React

The Redux library for React contains an associate strategy that permits you to interface a React part to a Redux store. The library considers the interface technique to be utilized as a decorator too.

//Before decorator
class MyApp extends React.Component {
// ...define your main app here
}
export default connect(mapStateToProps, mapDispatchToProps)(MyApp);//After decorator
@connect(mapStateToProps, mapDispatchToProps)
export default class MyApp extends React.Component {
// ...define your main app here
}

Besides, despite the fact that interface upholds the decorator grammar, it has been debilitate by the revival group right now. This is for the most part on the grounds that the decorator proposition in stage 2 can oblige changes later on.

I hope you enjoyed this article, so if it's not too much trouble, give your input with the goal that I can improve more.

Thank for reading , Stay blessed

Did you find this article valuable?

Support Shantun Parmar by becoming a sponsor. Any amount is appreciated!