mobx-react icon indicating copy to clipboard operation
mobx-react copied to clipboard

@inject cause Expected a constructor.

Open wangzmg opened this issue 7 years ago • 11 comments

i use reactNative

image Is ok

image

cause

image

Any good ideas?

wangzmg avatar Jun 05 '19 03:06 wangzmg

My configuration

image image

wangzmg avatar Jun 05 '19 03:06 wangzmg

Listening service image

wangzmg avatar Jun 05 '19 03:06 wangzmg

Same problem with fresh install of react-native, mobx and mobx-react. Did you find a workaround?

chanzxxx avatar Jun 07 '19 05:06 chanzxxx

maybe related to this issue ? https://github.com/facebook/react-native/issues/24452

chanzxxx avatar Jun 07 '19 05:06 chanzxxx

i getting same error :(

ispirLee25 avatar Jun 07 '19 21:06 ispirLee25

Yea, I would say it is definitely related to facebook/react-native#24452 and as such should be resolved there.

In case you don't want to wait for the fix, feel free to keep using mobx-react V5. As long as you write class components only, V6 doesn't give you much of the benefit.

danielkcz avatar Jun 08 '19 08:06 danielkcz

Hey everyone, here is a fix you can add yourself that will work with RN >= 0.59 or Expo SDK 33.

  1. Create rn-cli.config.js in your base folder.
module.exports = {
    transformer: {
        babelTransformerPath: require.resolve('./scripts/customReactNativeTransformer')
    }
};
  1. Create a file in your base folder called ./scripts/customReactNativeTransformer.js

Add the following:

const {
    transform,
    getCacheKey,
} = require('metro-react-native-babel-transformer/src/index);

function customTransform(config) {
    config.options.hot = false;
    return transform(config);
}

module.exports = {
    transform: customTransform,
    getCacheKey,
};
  1. Now you can use MobX@6!

stevelizcano avatar Jun 25 '19 19:06 stevelizcano

I tried the solution provided by @stevelizcano , but it didn't work for me.

I did the following:

  • Created file rn-cli.config.js, created folder script and created file customReactNativeTransformer.js in this folder;

  • I tried to install npm dependencies again, pod install and start (using commands rm -rf node_modules/, npm i, pod install and npm start -- --reset-cache), start iOS project using Xcode;

But the problem still exists, showing Expected a constructor.

Some configurations:

  • package.json:

    • {
        // some settings such as name, version are not shown here
        "dependencies": {
          "@babel/plugin-proposal-decorators": "^7.4.4",
          "babel-preset-react-app": "^9.0.0",
          "mobx": "^5.10.1",
          "mobx-react": "^6.1.1",
          "prop-types": "^15.7.2",
          "react": "16.8.3",
          "react-native": "0.59.9",
          // some other dependencies that I think not related to this issue
        },
        "devDependencies": {
          "jest": "^24.8.0",
          "react-test-renderer": "16.8.3"
        },
        "jest": {
          "preset": "react-native"
        },
        "babel": {
          "plugins": [
            [
              "@babel/plugin-proposal-decorators",
              {
                "legacy": true
              }
            ]
          ],
          "presets": [
            "react-app"
          ]
        }
      }
      
  • babel.config.js

    • module.exports = {
        presets: ['module:metro-react-native-babel-preset']
      };
      
  • jsconfig.json

    • {
          "compilerOptions": {
              "experimentalDecorators": true
          }
      }
      

Are there any important settings that haven't done here? Thank you!

fengh16 avatar Jun 26 '19 23:06 fengh16

Folks, this isn't really a bug caused by MobX (unless proven otherwise) and such you should be discussing it in react-native community. I appreciate posting pointers for passers-by, but let's not dwell into "it doesn't work for me" here. Thanks :)

danielkcz avatar Jun 27 '19 06:06 danielkcz

my solution:

class Test extends Component {
    ···
}

export default inject("appStore")(observer(Test));

qazse1234567 avatar Jul 05 '19 06:07 qazse1234567

Hey everyone, here is a fix you can add yourself that will work with RN >= 0.59 or Expo SDK 33.

  1. Create rn-cli.config.js in your base folder.
module.exports = {
    transformer: {
        babelTransformerPath: require.resolve('./scripts/customReactNativeTransformer')
    }
};
  1. Create a file in your base folder called ./scripts/customReactNativeTransformer.js

Add the following:

const {
    transform,
    getCacheKey,
} = require('metro-react-native-babel-transformer/src/index);

function customTransform(config) {
    config.options.hot = false;
    return transform(config);
}

module.exports = {
    transform: customTransform,
    getCacheKey,
};
  1. Now you can use MobX@6!

hot reload will not available with this config

ardyfeb avatar Sep 23 '19 09:09 ardyfeb