简体   繁体   English

得到错误:“react.createclass不是函数”

[英]Getting error: “react.createclass is not a function”

I am a newer JS developer and I am exploring React-Native. 我是一个较新的JS开发人员,我正在探索React-Native。 I recently purchased "Learning React Native" by Bonnie Eisenman and am working on the very first mini-project - a very basic weather app. 我最近购买了Bonnie Eisenman的“Learning React Native”,我正在开发第一个迷你项目 - 一个非常基本的天气应用程序。

Unfortunately, I can't get the code the author provides to work (see below for some of the code, or see the whole thing here ). 不幸的是,我无法获得作者提供的代码(参见下面的一些代码,或者在这里看到整个代码)。 I keep getting the error "React.createClass is not a function", even though I've seen dozens of other examples where this works just fine. 我一直得到错误“React.createClass不是一个函数”,尽管我已经看到了许多其他例子,其中这个工作正常。 I've been spending hours researching and asking questions with no success. 我花了几个小时研究并提出问题却没有成功。 Please help! 请帮忙!

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


var WeatherProject = React.createClass({
  ....
});

module.exports = WeatherProject;

according to the react-native docs https://facebook.github.io/react-native/ 根据react-native docs https://facebook.github.io/react-native/

You should require React from 'react' not 'react-native' 你应该要求React从'react'而不是'react-native'

And require your StyleSheet, Text, View, TextInput from 'react-native' (like you're currently doing) 并要求你的StyleSheet,Text,View,TextInput来自'react-native'(就像你现在正在做的那样)

from the docs: 来自文档:

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

class App extends Component {
  render() {
    return (
      <TabBarIOS>
        <TabBarIOS.Item title="React Native" selected={true}>
          <NavigatorIOS initialRoute={{ title: 'React Native' }} />
        </TabBarIOS.Item>
      </TabBarIOS>
    );
  }
}

This is using ES6 / ES2015 syntax of extending the react Component. 这是使用扩展react组件的ES6 / ES2015语法。 Yours will look more like this: 你的看起来会更像这样:

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


var WeatherProject = React.createClass({
  ....
});

module.exports = WeatherProject;

Although based on the object destructuring you're doing you are already in an environment that supports ES6 and it may be better to just use ES6 instead. 虽然基于您正在进行的对象解构,您已经处于支持ES6的环境中,但最好只使用ES6。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM