3 回答

TA贡献1812条经验 获得超5个赞
如果你的组件是一个你不应该使用的函数this.state.
,但你必须直接调用状态名称。
在您的代码中:
var bgColor = color.interpolate({...})
代替:
var bgColor = this.state.color.interpolate({...})
从反应文档
阅读状态
当我们想在一个类中显示当前计数时,我们读取 this.state.count:
<p>You clicked {this.state.count} times</p>
在函数中,我们可以直接使用count:
<p>You clicked {count} times</p>

TA贡献1825条经验 获得超4个赞
这是一个示例,不要为动画值创建状态,而是使用备忘录对其进行一次初始化并使用计时功能对其进行更新
小吃:https ://snack.expo.io/GwJtJUJA0
代码:
export default function App() {
const { value } = React.useMemo(
() => ({
value: new Animated.Value(0),
}),
[]
);
React.useEffect(() => {
Animated.loop(
Animated.sequence([
Animated.timing(value, {
toValue: 1,
duration: 1000,
}),
Animated.timing(value, {
toValue: 0,
duration: 1000,
})
])
).start();
}, []);
const backgroundColor = value.interpolate({
inputRange: [0, 1],
outputRange: ['#0dff4d', '#ff390d'],
});
return (
<View style={styles.container}>
<Animated.View style={{ width: 200, height: 100, backgroundColor }} />
</View>
);
}

TA贡献2051条经验 获得超10个赞
在功能组件中,您不使用 访问组件的状态属性/变量this.state.abc,而是直接使用状态变量的名称。所以你应该做的是:
var bgColor = color.interpolate({
inputRange: [0, 300],
outputRange: ['rgba(255, 0, 0, 1)', 'rgba(0, 255, 0, 1)']
});
添加回答
举报