user5815862
user5815862

Reputation:

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

I just added

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

to my index.ios.js file but when I reload my React Native application, this comes up:

It says React is read-only?!

So I tried to change the permissions on the react-native module by doing

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

but it still doesn't work. What did I do wrong?


This is the full source code of my index.ios.js file:

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

Upvotes: 2

Views: 1450

Answers (1)

d-vine
d-vine

Reputation: 5547

You actually import react-native two times. The second time around it complains. Just get rid of

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

This does the same thing but with the ES6 module syntax

import React from 'react-native'; 

Upvotes: 4

Related Questions