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

标签 javascript reactjs react-native

我刚刚添加

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

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

It says React is read-only?!

所以我尝试通过以下方式更改react-native模块的权限

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'; 

关于javascript - 我刚刚添加了 var React = require ('react-native' );我的整个构建失败了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34900399/

相关文章:

javascript - 没有回调或 promise 的异步函数的错误处理

android - Fresh Detached Expo + RNFirebase 未在 Android 上运行

javascript - 如何在 Emberjs 中将事件从内部组件冒泡到应用程序 Controller ?

javascript - 如何使用多个验证码获取验证码值?

javascript - 在 Jest 的函数内部模拟函数调用

react-native - 为 react-navigation DrawerNavigator 设置滑动区域宽度

javascript - 在 native react 中制作响应按钮

javascript - 当我将函数传递给 React.js 中的子组件时,为什么会出现未捕获的 TypeError?

CSS 模块和 CSS 关键帧动画

javascript - React.js 调用组件进入setState