如何在React Native中调用函数

时间:2019-06-04 16:59:05

标签: javascript android reactjs 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:未定义不是对象(正在评估   '_this.imageReference = _react.default.createRef()')

1 个答案:

答案 0 :(得分:3)

我相信您在构造函数中缺少super()才能使实例属性正常工作:

constructor() {
    super()
    this.imageReference = React.createRef();
}

此SO答案详细说明了原因: How to extend a class without having to using super in ES6?