Fixing React.createClass is not a function in React Native

React.createClass is not a function

React Native is still a fast-growing technology. That brings a good feeling of progress in the world of hybrid apps. But it also leads to new errors in your old working code. Among them: React.createClass is not a function in version 0.25.

Common case of React.createClass is not a function

The basic example of reproducing this error is when you creating project from scratch using outdated guides.

When I found this issue I was creating a new project using react-native init command. Then I replaced the ES6 code with a plain JS code, specified in a guide and it caused an error React.createClass is not a function.

Error: Seems you’re trying to access ‘ReactNative.createClass’ from the ‘react-native’ package.

Since version 0.25.1 the error React.createClass is not a function is replaced with an much more informative one:

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

For example, instead of:

  import React, { Component, View } from 'react-native';

You should now do:

  import React, { Component } from 'react';
  import { View } from 'react-native';

This basically tells you to use ES6 syntax for your apps.

If you don’t want to switch to ES6 syntax – downgrade react-native to the version that supports plain JS syntax (v. 0.24). If you know the workaround, please, let me know in the comments below.

Otherwise just use ES6 for your components and follow the instructions for correct imports.

Leave a Reply

Be the First to Comment!