javascript - 如何在 React Native 中调用函数

标签 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>
    )
}

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/

相关文章:

javascript - 我需要一个 "do not show again"按钮

meteor + react : Server side routes?

javascript - 如何在状态中使用来自 Fetch 请求的数据?

Javascript JSON 显示主题标签符号

javascript - jQuery.eCalendar() - 对事件使用 for 循环

javascript - AngularJs:orderBy 的表达式对我不起作用

android - 为什么日期选择器会在我的 View 中添加日历?

android - 如果屏幕关闭,警报管理器将无法工作

android - 0dp 大小的可组合项甚至可以组合吗

javascript - React.js : How to append a component on click?