React native:组件未定义?无法导入?

sky*_*guy 5 javascript reactjs react-native react-native-ios

好吧,非常新的反应原生在这里,我试图非常简单地导入另一个.js文件,并render()让它在index.ios.js 中的主函数中运行

我到处寻找并试图import and require这样做,但我遇到了错误:

组件未定义

这就是我所拥有的,只是添加了导入行就会出现错误:

import React, { Component } from 'react';
import { Button, Card } from 'react-native-material-design';
import {
  StyleSheet,
  Text,
  View,
  Animated,
  Easing,
  ScrollView,
  RefreshControl,
  AppRegistry
} from 'react-native';
//import { Container, Content } from 'native-base';

import TestClass from "./TestClass";
//var animation = require('./TestClass');

//BODY
export default class SkysReact extends Component {


  render() {
    return (<View style={styles.container}>
    <TestClass/>
    </View>);

    // return (<View style={styles.container}>
    // {this.test()}
    // </View>);
  }
  test() {
  console.log("Hello World")
}

animate()
{
  console.log("animate");
}
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#404040',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
    color: '#333333'
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

AppRegistry.registerComponent('SkysReact', () => SkysReact);
Run Code Online (Sandbox Code Playgroud)

而我的另一课:

import React from 'react';
import Animation from 'lottie-react-native';
import {
  StyleSheet,
  Text,
  View,
  Animated,
  Easing,
  ScrollView,
  RefreshControl,
  AppRegistry
} from 'react-native';

export default class TestClass extends Component { // not defined error here

    render() {
      return (<View style={styles.container}>
      {this.test()}
      </View>);
    }
    test() {
    console.log("Hello World 2222")
  }
}
module.exports = TestClass;
Run Code Online (Sandbox Code Playgroud)

我怎样才能在index.ios.js中显示TestClass?怎么了?

Dan*_*ich 9

啊哈.我确切地知道它是什么.将TestClass文件的最顶行与我的下面进行比较.你会看到差异.解决这个问题,你完成了.

import React, {Component} from 'react';
import Animation from 'lottie-react-native';
import {
  StyleSheet,
  Text,
  View,
  Animated,
  Easing,
  ScrollView,
  RefreshControl,
  AppRegistry
} from 'react-native';
export default class TestClass extends Component {

    render() {
      return (<View style={styles.container}>
      {this.test()}
      </View>);
    }
    test() {
    console.log("Hello World 2222")
  }
} 
Run Code Online (Sandbox Code Playgroud)

您错过了import语句中的{Component}.我也拿了你的module.exports语句,这是不必要的.