简体   繁体   English

失败的道具类型:提供给“RCTView”的“对象”类型的无效道具“不透明度”

[英]Failed prop type: Invalid prop `opacity` of type `object` supplied to `RCTView`

I'm following the React Native tutorial from: https://facebook.github.io/react-native/docs/animated.html我正在关注以下 React Native 教程: https://facebook.github.io/react-native/docs/animated.html

However, I got the following warning when I ran my code: Failed prop type: Invalid prop opacity of type object supplied to RCTView但是,我在运行代码时收到以下警告:道具类型失败:提供给RCTViewobject类型的道具opacity无效

And the component just disappear without animation when fade() got called.当调用 fade() 时,组件会在没有 animation 的情况下消失。

And here is a sample code:这是一个示例代码:

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>
    );
  }
......
}

You should change the View to an Animated.View. 您应该将视图更改为Animated.View。 Then optionally you can add an interpolated value of fadeAnim for more fine grained control. 然后,您可以选择添加fadeAnim的插值以进行更细粒度的控制。

Something like this should work... 像这样的东西应该工作......

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>
    );
  }

You may not want to fade the container view, in which case move the Animated.View inside the Touchable. 您可能不想淡化容器视图,在这种情况下将Animated.View移动到Touchable中。

Try the opacity using alpha-value of the background color instead. 尝试使用背景颜色的alpha值代替不透明度。

<Icon name="circle" size={30} color="#fff" style={{opacity: fadeAnim}} />

to

 ... let faceRgb = 'rgba(0,0,0,' + faceAnim + ' )'; return ( ... <Icon name="circle" size={30} color="#fff" style={{backgroundColor: faceRgb}} /> ... ) 

I had a similar problem but mine was because I was trying to use a stylesheet object as below我有一个类似的问题,但我的问题是因为我试图使用如下样式表 object

export const Skeleton = () => {
    const opacity = useRef(new Animated.Value(0));
    const styles = StyleSheet.create({
        skeleton: {
            flex: 1,
            backgroundColor: `rgb(210, 210, 210)`,
            position: 'relative',
            borderRadius: Number(borderRadius),
            width: width,
            height: height,
            opacity: opacity.current
        }
    })
    useEffect(() => {
        Animated.loop(
            Animated.sequence([
                Animated.timing(opacityVal, { toValue: 0.3, duration: 500, useNativeDriver: true }),
                Animated.timing(opacityVal, { toValue: 1, duration: 400, useNativeDriver: true  })
            ])
        ).start()
    }, [opacity])
    return (
        <Animated.View style={ styles.skeleton } />
    )
}

So I had to remove opacity from the stylesheet object and add it directly to the JSX like this所以我不得不从样式表 object 中删除不透明度并将其直接添加到 JSX 中,如下所示

return (
    <Animated.View style={[styles.skeleton, { opacity: opacity.current }]} />
)

I kept the other css in the object for readability and used an array in the style prop so I could add the opacity in addition to the other css.我将另一个 css 保留在 object 中以提高可读性,并在样式道具中使用了一个数组,因此除了其他 css 之外,我还可以添加不透明度。

There is a problem in your code: 您的代码中存在问题:

Icon tag can not be used in animcation, so react native complaint. 图标标签不能用于animcation,因此反应原生投诉。

Instead, you should wrap the Icon with either: TouchableHighlight, TouchableNativeFeedback, TouchableOpacity, TouchableWithoutFeedback whichever works. 相反,你应该用以下两种方式包装Icon:TouchableHighlight,TouchableNativeFeedback,TouchableOpacity,TouchableWithoutFeedback,无论哪种方法都有效。

https://facebook.github.io/react-native/docs/touchablewithoutfeedback.html https://facebook.github.io/react-native/docs/touchablewithoutfeedback.html

And bind the property to the Touchable* rather than Icon. 并将属性绑定到Touchable *而不是Icon。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应警告:失败的道具类型:提供的“对象”类型的无效道具 - React Warning: Failed prop type: Invalid prop of type `Object` supplied 失败的道具类型:提供给“TextInput”的“对象”类型的道具“值”无效 - Failed prop type: Invalid prop 'value' of type 'object' supplied to 'TextInput' 失败的道具类型:提供给“重定向”的无效道具“到” - Failed prop type: Invalid prop `to` supplied to `Redirect` 警告:失败的道具类型:提供给&#39;RCTView&#39;的props.style键&#39;resizeMode&#39;无效 - Warning: Failed prop type: Invalid props.style key 'resizeMode' supplied to 'RCTView' 提供的道具类型无效 - Invalid prop type supplied 道具类型失败:提供给“输入”的值“搜索”无效的道具“类型” - Failed prop type: Invalid prop `type` of value `search` supplied to `Input` 失败的道具类型:提供给`GlobalState`的`array`类型的无效道具`children` - Failed prop type: Invalid prop `children` of type `array` supplied to `GlobalState` 失败的道具类型:提供给“组”的“函数”类型的无效道具“元素”,预期为“对象” - Failed prop type: Invalid prop `element` of type `function` supplied to `Group`, expected `object` 失败的道具类型:提供给“ButtonBase”的“object”类型的无效道具“onClick”,预期为“function” - Failed prop type: Invalid prop `onClick` of type `object` supplied to `ButtonBase`, expected `function` 道具类型失败:提供给“按钮”的对象类型无效的道具“标签”,应为“字符串” - Failed prop type: Invalid prop `label` of type `object` supplied to `Button`, expected `string`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM