我刚刚添加了 var React = require('react-native');我的整个构建失败了

I just added var React = require('react-native'); and my whole build failed

本文关键字:我的 失败 构建 react-native var require 添加 React      更新时间:2023-09-26

我刚刚添加了

var React = require('react-native');

到我的 index.ios.js 文件,但是当我重新加载我的 React Native 应用程序时,这出现:

它说 React 是只读的?!

所以我试图通过

sudo chmod -R 777 ExampleProject
sudo chmod -R 777 ExampleProject/node_modules/react-native

但它仍然不起作用。我做错了什么?


这是我的index.ios.js文件的完整源代码:

 * Sample React Native App
 * https://github.com/facebook/react-native
 */
 'use strict';
 var React = require('react-native');
 import React, {
   AppRegistry,
   Component,
   StyleSheet,
   Text,
   View
 } from 'react-native';
 class Logbook extends Component {
   render() {
   return (
   <View style={styles.container}>
    <Text style={styles.welcome}>
      Welcome to React Native!
    </Text>
    <Text style={styles.instructions}>
      To get started, edit index.ios.js
    </Text>
    <Text style={styles.instructions}>
      Press Cmd+R to reload,{''n'}
      Cmd+D or shake for dev menu
    </Text>
  </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,
 },
 });
 AppRegistry.registerComponent('Logbook', () => Logbook);

您实际上导入了两次 react-native。第二次抱怨。只是摆脱

var React = require('react-native'); 

这执行相同的操作,但使用 ES6 模块语法

import React from 'react-native';