How to allow react-native to enable support for JSX (extension) files

后端 未结 3 704
小鲜肉
小鲜肉 2021-02-13 02:03

React Native app fails to resolve components.

I am trying to import & render Test.jsx inside of App.js.

I get the following error-<

相关标签:
3条回答
  • 2021-02-13 02:34

    It seems that the config schema was changed in 0.57 version: #248

    Try this:

    // ./rn-cli.config.js
    module.exports = {
      resolver: {
        sourceExts: ['jsx', 'js'],
      },
    };
    
    0 讨论(0)
  • 2021-02-13 02:52

    I am using react-native 0.59 and metro-react-native-babel-preset": "^0.53.0",.

    The ./rn-cli.config.js file no longer works in the IOS release build. RN 0.59 requires a config file called metro.config.js in the root level. I have to use it to enable JSX support instead of rn-cli.config.js. Check out the documentation for the config file: https://facebook.github.io/metro/docs/en/configuration.html

    /**
     * Metro configuration for React Native
     * https://github.com/facebook/react-native
     *
     * @format
     */
    
    module.exports = {
        transformer: {
            getTransformOptions: async () => ({
                transform: {
                    experimentalImportSupport: false,
                    inlineRequires: false,
                },
            }),
        },
        resolver: {
            sourceExts: ['jsx','js', 'json', 'ts', 'tsx']
        }
    };
    
    0 讨论(0)
  • 2021-02-13 02:56

    update: for RN >0.59 as @RedGaint pointed in https://stackoverflow.com/a/55134051/8034782 you need to configure metro.config.js in the root level.

      module.exports = {
      resolver: {
        /* resolver options */
       sourceExts: ['jsx','js'] //add here 
      },
      transformer: {
        /* transformer options */
      },
      serializer: {
        /* serializer options */
      },
      server: {
        /* server options */
      }
    
      /* general options */
    };
    

    For RN < 0.57: Inside of the root of your project add a file named rn-cli.config.js and place the following code into it.

    // ./rn-cli.config.js
    module.exports = {
      /// @description Allows you to enable support for JSX files
      /// The `index.js` file in the root of your project has to be `.js`. 
      getSourceExts: () => [ 'jsx', 'js' ],
    }
    

    For RN > 0.57:

      module.exports = {
      resolver: {
        sourceExts: ['jsx', 'js'],
        },
      };
    

    for more reference look into this there is already an issue opened:

    https://github.com/facebook/react-native/pull/5233#issuecomment-382083236

    0 讨论(0)
提交回复
热议问题