出于某种原因,我无法看到我的代码出错了。我似乎正在使用Animated,正如文档所示,但这个错误不断出现。 代码段:
import React, {
Component
} from 'react';
import {
StyleSheet,
Image,
Animated,
} from 'react-native'
import Header from './../components/Header'
export default class DrawerShell extends Component {
constructor(props) {
super(props)
this.state = {
showNav: false,
}
this.anim = new Animated.Value(0)
this.openDrawer = this.openDrawer.bind(this)
}
openDrawer() {
let toValue
this.setState({
showNav: !this.state.showNav
}) !this.state.showNav ? toValue = 1 : toValue = 0
Animated.timing( // Animate value over time
this.anim, // The value to drive
{
toValue: 1, // Animate to final value of 1
duration: 300,
}
).start()
}
render() {
let {
showNav
} = this.state
return ( <
Animated.View style = {
[
styles.appContainer,
{
transform: [{
translate: [
this.anim.interpolate({
inputRange: [0, 1],
outputRange: [0, 200],
}),
this.anim.interpolate({
inputRange: [0, 1],
outputRange: [0, 80],
}),
0
]
},
{
scale: this.anim.interpolate({
inputRange: [0, 1],
outputRange: [1, 0.7]
})
}
]
},
]
} >
<
Image source = {
{
uri: "splash_bg"
}
}
style = {
styles.bgImage
} >
<
Header title = "hi there"
onPress = {
this.openDrawer
}
/> <
/Image>
</Animated.View>
);
}
}
&#13;
答案 0 :(得分:3)
对于其他来自Google的人可能有用。确保在Animated.View
之类的动画组件中使用动画值。当将视图“升级”为动画时,通常会被忽略。
答案 1 :(得分:2)
想通。抛出此错误的两个原因。
一旦我停止对同一个值进行多次插值并使其独立于状态,错误就消失了。
答案 2 :(得分:2)
对于那些来这里的人,您需要在<Animated.View>
内添加 动画值 !
请参阅此问题: https://github.com/facebook/react-native/issues/10716#issuecomment-258098396
答案 3 :(得分:0)
我认为这可能会在返回运算符中将值直接赋值给状态对象,并且错误
openDrawer() {
let toValue
this.setState({
showNav: !this.state.showNav
})
toValue = (!this.state.showNav) ? 1 : 0 // change this this line case error
Animated.timing( // Animate value over time
this.anim, // The value to drive
{
toValue: 1, // Animate to final value of 1
duration: 300,
}
).start()
}