首页 > 解决方案 > 在 React Native 渲染文本组件中显示动画值

问题描述

我无法在渲染上显示 Animated 的值并返回此错误。

Invariant Violation:对象作为 React 子级无效(发现:带有键 {value} 的对象)。如果您打算渲染一组子项,请改用数组。

当然,我看到了价值console

constructor(props) {
    super(props);

    this.state={
       progress:0
    }

    this.animatedValue = new Animated.Value(0);

    this.animatedValue.addListener((progress) => {
        this.setState({progress})
    });
}

componentDidMount() {
    this.animate()
}

animate() {
    this.animatedValue.setValue(0);
    Animated.timing(
        this.animatedValue,
        {
            toValue: 1,
            duration: 15000,
            easing: Easing.linear
        }
    ).start()
}

render() {
    return(
        <View>
            <Text> {this.state.progress} </Text>
        </View>
    );

}

标签: javascriptreactjsreact-native

解决方案


给定的函数addListener将使用带有value键作为参数的对象调用,因此不要progress使用整个对象进行设置,value而是使用:

this.animatedValue.addListener((progress) => {
  this.setState({ progress: progress.value });
});

推荐阅读