javascript - React native : Component not defined? 无法导入?

标签 javascript reactjs react-native react-native-ios

好的,这里对 native react 非常陌生,我正在尝试非常简单地导入另一个 .js 文件,并让它在 index.ios.js 的主要 render() 函数中运行

我到处寻找并尝试了 import 和 require 来执行此操作,但是我遇到了错误:

Component is not defined

这是我所拥有的,错误是在添加导入行时抛出的:

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

还有我的其他类(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;

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

最佳答案

啊哈。我很清楚那是什么。将你的 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")
  }
} 

您在导入语句中遗漏了 {Component}。我还接受了您的 module.exports 声明,这是不必要的。

关于javascript - React native : Component not defined? 无法导入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45089859/

相关文章:

javascript - XMLHttpRequest 同步或异步

javascript - 单击包含 span 的 div,不会触发 onclick 事件 - React

android - "Fetching JS Bundle"非常慢

javascript - onkeydown 和 onkeyup 事件在 Internet Explorer 8 上不起作用

javascript - 如何在 sequelize v6 中删除自动创建的列 ID?

javascript - 在 D3.js 中调用函数作为数据

javascript - 在 React 中发送电子邮件通知并在 Firebase 中保存数据库

javascript - React 中的 Axios AJAX 调用仅返回初始状态

ios - 有没有办法在iOS中使用react-native来制作像Android StaggeredGridView这样的布局

react-native - 如何在 native react 中使用 Iframe?