【问题标题】:I just added var React = require('react-native'); and my whole build failed我刚刚添加了 var React = require('react-native');我的整个构建失败了
【发布时间】:2016-01-20 12:42:32
【问题描述】:

我刚刚加了

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

【问题讨论】:

  • 你能把你的index.ios.js文件的所有代码都包含进去吗?

标签: javascript reactjs react-native


【解决方案1】:

您实际上两次导入 react-native。第二次围绕它抱怨。摆脱掉

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

这做同样的事情,但使用 ES6 模块语法

import React from 'react-native'; 

【讨论】:

  • 谢谢!但是为什么他们在官方文档中实现了两次呢?为什么它说'React'是只读的? @dvine
  • 我认为您刚刚在文档中发现了一个错误 :) 也许您应该打开一个问题或提交 PR。如果你愿意,我可以做到。我不是 100% 确定错误,但我最好的猜测是,模块在某处生成全局常量。通常,当您两次导入相同的模块时,它只会从缓存中获取已经导入的模块。但是由于你使用了两个不同的模块加载器来加载同一个模块,所以这不知何故不起作用。
猜你喜欢
  • 1970-01-01
  • 2023-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-28
  • 2019-11-21
  • 1970-01-01
  • 2023-03-06
相关资源
最近更新 更多