javascript - 如何在 React Native 中调用函数
问题描述
不能通过引用使用子函数。
export class Home extends React.Component {
constructor() {
this.imageReference = React.createRef();
}
state = {
fadeAnim: new Animated.Value(0),
}
componentDidMount() {
Animated.timing(
this.state.fadeAnim,
{
toValue: 1,
duration: 2400,
useNativeDriver: true
}
).start(() => this.imageReference.current.initImageAnimation());
}
render() {
let { fadeAnim } = this.state;
return (
<View style={{ flex: 1 }}>
<Animated.View style={{
opacity: fadeAnim,
flex: 1,
backgroundColor: '#7eb3e0',
justifyContent: 'center',
alignItems: 'center'
}}>
<Logo ref={this.imageReference} />
</Animated.View>
</View>
)
}
发生错误:TypeError: undefined is not an object (evalating '_this.imageReference = _react.default.createRef()')
解决方案
我相信您在构造函数中缺少 super() 以使您的实例属性起作用:
constructor() {
super()
this.imageReference = React.createRef();
}
这个 SO 答案详细解释了为什么: 如何在 ES6 中无需使用 super 来扩展类?
推荐阅读
- datastax - 将 LocalDateTime 转换为 Cassandra TIMESTAMP 时的编解码器问题
- python - Python程序打印出提供的子字符串的模式在提供的字符串中匹配的次数
- node.js - Mongodb _id 在同一个集合中有两种不同的数据类型
- powershell - 如何找到有权访问共享邮箱的特定用户?
- python - PySpark df.write 正在将不同数量的记录加载到不同的表中
- r - 识别在数据框中复制的行
- android - NavigationUI.navigateUp 什么都不做
- c# - 安装驱动程序后检测新的 USB 电缆/设备
- angular - Angular 虚拟滚动和 Browseranimation Renderbug
- javascript - 为什么我无法访问此 DataTable 中隐藏列的文本?