首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误:实现react animation.timing方法时出错

错误:实现react animation.timing方法时出错
EN

Stack Overflow用户
提问于 2020-04-14 14:50:44
回答 1查看 82关注 0票数 0

由于某些原因,我看不到我的代码做错了什么。正如文档显示的那样,我似乎在使用Animated,但这个错误总是出现。代码片段:

代码语言:javascript
运行
复制
    import React ,{ Component} from "react";
    import { Card } from 'react-native-elements';
    import { Text, View ,Animated,Easing} from 'react-native';
    import { connect } from 'react-redux';
    import { baseUrl } from '../shared/baseUrl';
    import { Loading } from './LoadingComponent';

    const mapStateToProps = state => {
        return {
          dishes: state.dishes,
          comments: state.comments,
          promotions: state.promotions,
          leaders: state.leaders
        }
      }

function RenderItem(props) {
    const item = props.item;
        if (item != null) {
            return(
                <Card
                    featuredTitle={item.name}
                    featuredSubtitle={item.designation}
                    image={{uri: baseUrl + item.image}}>
                    <Text
                        style={{margin: 10}}>
                        {item.description}</Text>
                </Card>
            );
        }
}

class Home extends Component{
    constructor(props) {
        super(props);
        this.animatedValue = new Animated.Value(0);        
    }
    componentDidMount () {
        this.animate()
    }
    animate () {
        this.animatedValue.setValue(0)
        Animated.timing(
          this.animatedValue,
          {
            toValue: 8,
            duration: 8000,
            easing: Easing.linear
          }
        ).start(() => this.animate())
    }
    render() {

        const xpos1 = this.animatedValue.interpolate({
            inputRange:[0,1,3,5,8],
            outputRange:[1200 , 600 , 0 , -600 , -1200]
        });

        return(
            <View style = {{flex :1 , flexDirection:'row' , justifyContent : 'center'}}>
                <Animated.View style={{width :'100%' , tranform :[{translateX:xpos1}]}}>
                    <RenderItem item={this.props.dishes.dishes.filter((dish) => dish.featured)[0]}
                        isLoading={this.props.dishes.isLoading}
                        erreMess={this.props.dishes.erreMess} 
                    />
                </Animated.View>


            </View>

        );
    }
}

export default connect(mapStateToProps)(Home);

我正在尝试实现animation.timing函数。正如文档中所提到的,我使用在构造函数中声明的animatedValue编写了animation.timing函数。但是当我编写动画函数时,我得到了以下错误:

我遵循了react动画的文档,并在互联网上尝试了所有方法来解决这个问题,但都失败了。

EN

回答 1

Stack Overflow用户

发布于 2020-04-14 15:48:39

我认为您可能只想循环动画,而不是尝试手动“重置”时间,这在文档中是不推荐的。

loop

连续循环给定的动画,以便每次到达末尾时,它都会重置并从头开始。

代码语言:javascript
运行
复制
constructor(props) {
  super(props);
  this.state = {
    animatedValue: new Animated.Value(0),
  } 
}

componentDidMount () {
  this.animate();
}

animate = () => {
  Animated.loop(
    Animated.timing(
      this.state.animatedValue,
      {
        useNativeDriver: true, // loop W/O blocking main JS thread
        toValue: 8,
        duration: 8000,
        easing: Easing.linear
      }
    ),
  ).start()
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61202131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档