React js - What is the difference betwen HOC and decorator

itay312 picture itay312 · Feb 8, 2018 · Viewed 7.3k times · Source

Can someone explain what is the difference between these two? I mean except the syntactic difference, do both of these techniques are used to achieve the same thing (which is to reuse component logic)?

Answer

Jeff P Chacko picture Jeff P Chacko · Feb 8, 2018

For all practical reasons, decorators and HOC's do the same thing.

One major difference is that, once you add a decorator, the property/class can only be used in it's decorated form. HOC pattern leaves higher order as well as the lower order components available for use.

For further reading on decorators -> https://medium.com/google-developers/exploring-es7-decorators-76ecb65fb841
Decorators isn't a widely implemented JS feature. It is still in its proposal stage. Babel 7 by default allows decorators as a default plugin in their stage 0 configuration. https://babeljs.io/docs/plugins/transform-decorators/