gpt4 book ai didi

reactjs - Prop 类型失败 : Invalid prop `opacity` of type `object` supplied to `RCTView`

转载 作者:行者123 更新时间:2023-12-03 14:04:14 25 4
gpt4 key购买 nike

我正在关注以下的 React Native 教程: https://facebook.github.io/react-native/docs/animated.html

但是,当我运行代码时,我收到以下警告:失败的 Prop 类型:提供给 RCTView

object 类型的 Prop opacity 无效

当调用 fade() 时,组件会消失,没有动画。

这是一个示例代码:

import React, { Component } from 'react';
import {
AppRegistry,
Text,
View,
Animated,
StyleSheet
} from 'react-native';

import Icon from 'react-native-vector-icon/FontAwesome'

export default class Sample extends Component {
state = {
fadeAnim: new Animated.Value(0),
}
fade() {
Animated.timing( // Animate over time
this.state.fadeAnim, // The animated value to drive
{
toValue: 1, // Animate to opacity: 1 (opaque)
duration: 10000, // Make it take a while
}
).start(); // Starts the animation
}
render() {
let { fadeAnim } = this.state;

return (
<View style={styles.container}>
<TouchableHighlight onPress={() => {this.fade()}}>
<Icon name="circle" size={30} color="#fff" style={{opacity: fadeAnim}}
>
</TouchableHighlight>
</View>
);
}
......
}

最佳答案

您应该将 View 更改为 Animated.View。然后,您可以选择添加 fadeAnim 的内插值以进行更细粒度的控制。

像这样的东西应该有效......

render() {
const { fadeAnim } = this.state;

// optionally tweak the input / output range to suit your needs
const opacity = fadeAnim.interpolate({
inputRange: [0, 1],
outputRange: [0, 1]
});

return (
<Animated.View style={[styles.container, opacity]}>
<TouchableHighlight onPress={() => {this.fade()}}>
<Icon name="circle" size={30} color="#fff"
>
</TouchableHighlight>
</Animated.View>
);
}

您可能不想淡出容器 View ,在这种情况下将 Animated.View 移动到 Touchable 内。

关于reactjs - Prop 类型失败 : Invalid prop `opacity` of type `object` supplied to `RCTView` ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45567343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com