admin管理员组文章数量:1421066
I am looking at how to animate colors in react native and followed this tutorial
All I have done is first run react-native init
then replace the code in my App.js with this
import { StyleSheet, View, Text, Animated } from 'react-native';
import React, { Component } from 'react';
export default class App extends Component {
ponentDidMount() {
this.animatedValue = new Animated.Value(0);
}
ponentDidMount() {
Animated.timing(this.animatedValue, {
toValue: 150,
duration: 1500
}).start();
}
render() {
const interpolateColor = this.animatedValue.interpolate({
inputRange: [0, 150],
outputRange: ['rgb(0,0,0)', 'rga(51,250,170)']
});
const animatedStyle = {
backgroundColor: interpolateColor
}
return (
<Animated.View style={[{ width: 50, height: 50 }, animatedStyle]} />
);
}
}
And then run react-native run-android
And now I keep getting TypeError:undefined is not an object(evaluating 'this.animatedValue.interpolate')
I am looking at how to animate colors in react native and followed this tutorial https://codedaily.io/screencasts/8/Animate-Colors-with-React-Native-Interpolate
All I have done is first run react-native init
then replace the code in my App.js with this
import { StyleSheet, View, Text, Animated } from 'react-native';
import React, { Component } from 'react';
export default class App extends Component {
ponentDidMount() {
this.animatedValue = new Animated.Value(0);
}
ponentDidMount() {
Animated.timing(this.animatedValue, {
toValue: 150,
duration: 1500
}).start();
}
render() {
const interpolateColor = this.animatedValue.interpolate({
inputRange: [0, 150],
outputRange: ['rgb(0,0,0)', 'rga(51,250,170)']
});
const animatedStyle = {
backgroundColor: interpolateColor
}
return (
<Animated.View style={[{ width: 50, height: 50 }, animatedStyle]} />
);
}
}
And then run react-native run-android
And now I keep getting TypeError:undefined is not an object(evaluating 'this.animatedValue.interpolate')
- There are a 'ponentDidMount' repeated. – PTT Commented Feb 8, 2018 at 9:00
1 Answer
Reset to default 2The ponentDidMount
lifecycle method only runs after the first render
. Therefore this.animatedValue
will be undefined when the ponent first mounts.
You can declare the animated value as an instance property on the ponent, so that it will be available from when the ponent is first created.
export default class App extends Component {
animatedValue = new Animated.Value(0)
//...
}
Additionally, you can't define multiple ponentDidMount
methods as you have here.
本文标签: javascriptUndefined is not an object animatedinterpolate react nativeStack Overflow
版权声明:本文标题:javascript - Undefined is not an object animated.interpolate react native - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745322179a2653427.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论