Skip to content

Instantly share code, notes, and snippets.

@sebnyberg
Created September 7, 2017 16:17
Show Gist options
  • Save sebnyberg/592b5b9e91d5c4266a5bffdd73f5f280 to your computer and use it in GitHub Desktop.
Save sebnyberg/592b5b9e91d5c4266a5bffdd73f5f280 to your computer and use it in GitHub Desktop.
How to setup decorators in React Native

Using decorators in React Native

  1. Install babel plugins to enable decorators.

    npm install babel-plugin-transform-decorators-legacy --save-dev
  2. Create or update your .babelrc file in root folder:

    {
     "presets": ["react-native"],
     "plugins": ["transform-decorators-legacy"]
    }
  3. Done! Now you can use decorator like:

    import {connect} from 'react-redux'
    
    @connect(...)
    class MyComponent extends React.Component{
        ...
    }
    export default MyComponent

Benifits of this

You don't have to scroll to the bottom of the file to see Redux's connect()

@Gxmadix
Copy link

Gxmadix commented Aug 20, 2022

Thanks, that's perfectly what I was searching for. you surely have saved me some troubles.

@Dreemmen
Copy link

Not working. Still says I need to install decorators plugin. Im using create-react-app and mobx

@sebnyberg
Copy link
Author

Not working. Still says I need to install decorators plugin. Im using create-react-app and mobx

This is 6 years old at this point. Given the pace of breaking changes in web dev I would be surprised if this still works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment