不能通过引用使用子项的函数。
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>
)
}
Error occures: TypeError: undefined is not an object (evaluating '_this.imageReference = _react.default.createRef()')
最佳答案
我相信您在构造函数中缺少 super() 以使您的实例属性起作用:
constructor() {
super()
this.imageReference = React.createRef();
}
这个 SO 答案详细解释了原因: How to extend a class without having to using super in ES6?
关于javascript - 如何在 React Native 中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56448340/