在React Native Render Text组件中显示动画值

Mah*_*our 3 javascript reactjs react-native

我无法在“渲染”上显示“动画值”并返回此错误。

永久违反:对象作为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>
    );

}
Run Code Online (Sandbox Code Playgroud)

Tho*_*lle 6

给定的函数addListener将使用带有value键作为参数的对象来调用,因此,progress与其设置整个对象,不如使用整个对象value

this.animatedValue.addListener((progress) => {
  this.setState({ progress: progress.value });
});
Run Code Online (Sandbox Code Playgroud)

  • 这将导致组件状态发生变化,因此重新渲染!,对吗?如果是,还有更好的主意吗? (2认同)