0

I have the following code in React Native from a Udemy tutorial I'm following. However, when I try to run it, I keep getting an error about createClass. I am not able to continue the tutorial since this doesn't run. How do I fix it?

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

var {
  Text,
  View,
  AppRegistry
} = React;

/*
var AppRegistry = React.AppRegistry;
var Text = React.Text;
var View = React.View;
// same as:
// var Text = React.Text;
// var View = React.View;
*/
var StopWatch = React.createClass({
  render: function() {


    return <View>
      <Text>
      00:00.00
      </Text>
      {this.startStopButon()}
      {this.lapButon()}
    </View>
  },
  startStopButon: function() {
    return <View>
    <Text>
      Start
    </Text>
    </View>
  },
  lapButon: function() {
    return <View>
    <Text>
      Lap
    </Text>
    </View>
  },

  }
});

/*
AppRegistry.registerComponent('stopwatch', function() {
  return StopWatch;
});
*/

AppRegistry.registerComponent('stopwatch', () => StopWatch);
mjpablo23
  • 681
  • 1
  • 7
  • 23

2 Answers2

0

I think you are using latest react-native version. so you need to use es6 class definition.

class C extends React.Component { render() { return ; } }

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

    var {
      Text,
      View,
      AppRegistry
    } = React;

    /*
    var AppRegistry = React.AppRegistry;
    var Text = React.Text;
    var View = React.View;
    // same as:
    // var Text = React.Text;
    // var View = React.View;
    */
export class StopWatch extends Component {  
      render () {


        return <View>
          <Text>
          00:00.00
          </Text>
          {this.startStopButon()}
          {this.lapButon()}
        </View>
      }
      startStopButon = () => {
        return <View>
        <Text>
          Start
        </Text>
        </View>
      }
      lapButon = () => {
        return <View>
        <Text>
          Lap
        </Text>
        </View>
      }

      }
    }

    /*
    AppRegistry.registerComponent('stopwatch', function() {
      return StopWatch;
    });
    */

    AppRegistry.registerComponent('stopwatch', () => StopWatch);
Sathish Sundharam
  • 1,060
  • 2
  • 12
  • 22
0

just enclose your return in parenthesis in case you do not have any version issues

return( <View>
      <Text>
      00:00.00
      </Text>
      {this.startStopButon()}
      {this.lapButon()}
    </View>
  },
  startStopButon: function() {
    return <View>
    <Text>
      Start
    </Text>
    </View>
  },
  lapButon: function() {
    return <View>
    <Text>
      Lap
    </Text>
    </View>
);
Vikram Saini
  • 2,713
  • 1
  • 16
  • 33