如何允许 react-native 启用对 JSX(扩展)文件的支持

IT技术 javascript reactjs react-native
2021-05-02 16:50:54

React Native 应用程序无法解析组件。

我正在尝试Test.jsxApp.js.

我收到以下错误-

error: bundling failed: Error: Unable to resolve module `./screens/Test` from App.js`:
The module `./screens/Test` could not be found from App.js. Indeed, none of these files exist

项目经理(或者更确切地说是文件)看起来像这样-

文件

Test.js 代码-

import React, { Component } from 'react';
import {View, Text, StyleSheet} from 'react-native'

export default class Test extends Component {
    render() {
      return (
        <View style={styles.container}>
          <Text>Hello World?</Text>
        </View>
      );
    }
  }


const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  }
});

App.js 代码-

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  Platform,
  StyleSheet,
  Text,
  View
} from 'react-native';

import Test from "./screens/Test";

const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' +
    'Cmd+D or shake for dev menu',
  android: 'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});

type Props = {};
export default class App extends Component<Props> {
  render() {
    return (
      <View style={styles.container}>
        <Test />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

我确实尝试了提到的所有解决方案 - react-native#4968,但似乎没有一个有效。我也提到了this,但是,解决方案是针对 css 的,与解决此问题的方法相去甚远。

我已经查看了更多问题,并且不知道我还需要做什么。我还创建了一个新项目(截图和代码来自新项目)。

我错过了什么?

更新: 我意识到这个问题是因为我有.jsx扩展。导入.js文件工作正常有关如何使项目接受.jsx导入的任何指示

4个回答

更新对于 RN >0.59正如@RedGaint 在https://stackoverflow.com/a/55134051/8034782 中指出的那样,您需要metro.config.js在根级别进行配置

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

  /* general options */
};

对于 RN < 0.57:在项目的根目录中添加一个名为 rn-cli.config.js 的文件并将以下代码放入其中。

// ./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' ],
}

对于 RN > 0.57

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

有关更多参考,请查看此问题已经打开了一个问题:

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

我正在使用react-native 0.59metro-react-native-babel-preset": "^0.53.0",

./rn-cli.config.js文件不再适用于 IOS 版本构建。RN 0.59在根级别需要一个名为Metro.config.js的配置文件我必须使用它来启用 JSX 支持而不是rn-cli.config.js. 查看配置文件的文档: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.57 版本中更改了配置架构:#248

试试这个:

// ./rn-cli.config.js
module.exports = {
  resolver: {
    sourceExts: ['jsx', 'js'],
  },
};

这适用于 ReactNative v0.63

// metro.config.js
const { getDefaultConfig } = require('metro-config');
module.exports = (async () => {
  const { resolver: { sourceExts } } = await getDefaultConfig();
  return { resolver: { sourceExts: [...sourceExts, 'jsx'] } };
})();

注意:此方法不会覆盖现有设置。