【发布时间】:2017-04-27 04:01:58
【问题描述】:
我正在研究 React-Native,我想从不同的类调用一个函数,但是当我尝试这样做时,它显示了一些错误。
A 类
import B from './B.js';
class A extends Component {
_onItemPressed(item){
B.abc();
}
render() {
return (
<TouchableHighlight
underlayColor={Colors.colors.lightgrey}
style={{padding: 15}}
onPress={this._onItemPressed.bind(this)}>
<Text>Click Me !</Text>
</TouchableHighlight>
);
}
}
B 类
class B extends Component {
abc(){
alert('Hello World');
}
render() {
return (
<View>
<Text>Welcome to React Native</Text>
</View>
);
}
}
但按下 A 类中的按钮后出现错误消息,'undefined is not a function (evalating 'B.default._abc()')'
请仔细阅读我的帖子并建议我一些解决方案。
谢谢
【问题讨论】:
-
您要直接使用组件类吗?您需要实例化该类或将方法更改为静态才能调用它。
-
嘿,谢谢,但我想让函数静态,没有静态你知道如何访问这个函数
标签: react-native