简体   繁体   English

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

[英]React native: Component not defined? Can't import?

Ok, very new to react native here and Im trying to very simply import another .js file and have that be run in the main render() func in index.ios.js 好的,非常新的在这里做出反应原因我想尝试非常简单地导入另一个.js文件并让它在index.ios.js中的主render()函数中运行

I have looked everywhere and tried both import and require to do this, however I am stuck with error: 我到处寻找并尝试import and require这样做,但是我遇到了错误:

组件未定义

Here is what I have, the error is thrown at just the addition of the import line: 这就是我所拥有的,只是添加了导入行就会出现错误:

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);

And my other class: 而我的另一课:

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;

How can I just display TestClass in my index.ios.js? 我怎样才能在index.ios.js中显示TestClass? What is wrong? 怎么了?

Ah hah. 啊哈。 I know exactly what it is. 我确切地知道它是什么。 Compare the very top line of your TestClass file with mine below. 将TestClass文件的最顶行与我的下面进行比较。 You will see the difference. 你会看到差异。 Fix this, and your done. 解决这个问题,你完成了。

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")
  }
} 

You were missing the, {Component} in your import statement. 您错过了import语句中的{Component}。 I also took our your module.exports statement, its unnecessary. 我也拿了你的module.exports语句,这是不必要的。

this.test() is not a valid child of your <View> in TestClass because it is not a valid React Component, nor does it return one. this.test()不是TestClass <View>的有效子this.test() ,因为它不是有效的React组件,也不返回一个。

If you want to "test" that your TestClass.render() function is running, put the console.log() above your return statement, like this: 如果你想“测试”你的TestClass.render()函数正在运行,请将console.log()放在return语句之上,如下所示:

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

Of course, you won't actually see anything because TestClass doesn't have any children. 当然,你实际上看不到任何东西,因为TestClass没有任何孩子。

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

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