dtjmsy
dtjmsy

Reputation: 2762

react native createClass error

I have this codes:

var React = require('react-native');
var {
  AppRegistry,
  MapView,
  View,
  StyleSheet
} = React;

var Weather = React.createClass({
  render: function(){
    return <MapView style={styles.map}></MapView>
  }

});

const styles = StyleSheet.create({
  map: {
    flex: 1,
  }
});

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

I just want to test the map, however, when I run it, I got the error:

Seems you're trying to access 'react-native' package. Perhaps you meant to access 'React.createClass' from the 'react' package instead ?

I don' t see what's wrong with the above codes, can you help me out

Thanks

Upvotes: 0

Views: 99

Answers (2)

Matt Aft
Matt Aft

Reputation: 8936

You're importing incorrectly. You need to also import React to be able to use React.createClass, right now you're naming React as the 'react-native' library:

var React = require('react');
var ReactNative = require('react-native');
var {
  AppRegistry,
  MapView,
  View,
  StyleSheet
} = ReactNative;

Upvotes: 1

Talor A
Talor A

Reputation: 85

I believe ES6 classes are the recommended way to create react components in React Native. to do that, your component should look something like this:

import React, { Component } from 'react'
import {
  AppRegistry,
  MapView,
  View,
  StyleSheet
} from 'react-native'

class Weather extends Component {
  render () {
    ...
  }
}

hope this helps.

Upvotes: 0

Related Questions