好的,这里对 native react 非常陌生,我正在尝试非常简单地导入另一个 .js 文件,并让它在 index.ios.js 的主要 render()
函数中运行
我到处寻找并尝试了 import 和 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);
还有我的其他类(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/