This is the best solution I've found without ejecting. Replace create-react-scripts.
https://www.npmjs.com/package/custom-react-scripts
If you have a new project start by using this in your terminal:
create-react-app my-app --scripts-version custom-react-scripts
Then cd into my-app directory and create a .env file. Edit the .env file to this:
REACT_APP_BABEL_STAGE_0=true
REACT_APP_DECORATORS=true
Your app will now support decorators.
If you have an existing app and want to add this. Add the .env like above then:
npm i custom-react-scripts --save
remove react-scripts in your package.json. Do not change the scripts from react-scripts start to create-react-scripts start. Leave the scripts as is.
now add your mobx stuff with decorators and enjoy less boilerplate code.
my final package.json
{
"name": "mobx-test",
"version": "0.1.0",
"private": true,
"dependencies": {
"custom-react-scripts": "^0.2.1",
"mobx": "^3.4.0",
"mobx-react": "^4.3.5",
"react": "^16.2.0",
"react-dom": "^16.2.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
oh and if you are using vscode you will want to hide the warnings about unsupported decorators. To do that add a tsconfig.json file to your project with this
{
"compilerOptions": {
"experimentalDecorators": true,
"allowJs": true
}
}