Search code examples
javascriptreactjscreate-react-app

Is it possible to use decorators with create-react-app?


I have understood that since Babel doesn't support decorators out of the box (because it's in early stages of definition) create-react-app doesn't support it as well.

I know that you can eject the generated app and configure Babel to support them, but I don't want to do that.

Finally, libraries like MobX allow you to use decorator's behavior without actually using them, with the help of some utility functions like described at https://mobx.js.org/best/decorators.html

Is there something similar for React?


Solution

  • Yes, you can, but you need a couple of things,

    Make sure you have react-app-rewired and customize-cra installed so you can override webpack and babel configs

    install @babel/plugin-proposal-decorators

    and update your config-overrides.js file :

    const { override, addBabelPlugin } = require("customize-cra");
    const pluginProposalDecorators = require("@babel/plugin-proposal-decorators");
    
    module.exports = override(  
      addBabelPlugin(pluginProposalDecorators)
    );