58

I am trying to make a simple iOS page with a button that triggers an action. I have followed the tutorial on how to get started and this is my index code:

'use strict';
var React = require('react-native');
var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableHighlight,
  Component,
  AlertIOS // Thanks Kent!
} = React;

class myProj extends Component {
 render() {
    return (
      <View style={styles.container}>
        <Text>
          Welcome to React Native!
        </Text>
        <TouchableHighlight style={styles.button}
            onPress={this.showAlert}>
            <Text style={styles.buttonText}>Go</Text>
          </TouchableHighlight>
      </View>
    );
  }
 
  showAlert() {
    AlertIOS.alert('Awesome Alert', 'This is my first React Native alert.', [{text: 'Thanks'}] )
  }
}

var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#FFFFFF'
  },
  buttonText: {
    fontSize: 18,
    color: 'white',
    alignSelf: 'center'
  },
  button: {
    height: 44,
    flexDirection: 'row',
    backgroundColor: '#48BBEC',
    alignSelf: 'stretch',
    justifyContent: 'center'
  }
});

AppRegistry.registerComponent('myProj', () => myProj);

The problem is that when I run it from Xcode on my device I get

Can't find variable: React
render
main.jsbundle:1509:6
mountComponent
mountChildren

Any idea what might be the problem here?

3
  • try to run your project with react-native start or react-native run-ios commands Commented Jul 31, 2016 at 16:10
  • I tried and it's showing the same error message Commented Jul 31, 2016 at 16:19
  • what's your react-native version? Commented Jul 31, 2016 at 16:20

4 Answers 4

133

In the latest version of React Native you must import React from 'react' package

import React, {Component} from 'react';
import {
View,
...
} from 'react-native';
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you! Just adding that is should be wherever jsx is used
@user269867 if you split your app as stackNavigator, bottomTabNavigator etc., check the navigator components.
Do I have to do this in EVERY file?
omg thank you. Yes, I think this does need to be in every file, since jsx is just sugar which underlying it has dependencies on the React object
Furthermore, it has to be import React from 'react', and on every file
|
7
import * as React from 'react';

Adding this within all of the files in which you have JSX should fix the problem.

If you are having trouble finding which files you need add it to (XCode error can be unclear), running react-native run-ios will give you a more precise location for the error.

2 Comments

Holy hell that finally solved that annoying error
it gets removed when i press ctrl + shift + O to remove other unwanted imports
0

Add the constructor to before render

constructor(props) {

super(props); }

Comments

0

Adding import React from 'react'; in all the jsx/tsx file resolved my issue

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.