【问题标题】:React Native Image error Failed prop type: Invalid prop source supplied to ForwardRef(Image)React Native Image错误道具类型失败:提供给ForwardRef(Image)的道具源无效
【发布时间】:2019-08-05 23:10:52
【问题描述】:

在我的 react-native 应用程序中,我有一个包含 Image 组件的组件数组,但我有这个错误

警告:道具类型失败:提供给的道具来源无效 ForwardRef(图像)。

文件活动预览

render() {
    return (
        <TouchableOpacity>
            <ImageBackground
                source={ require(this.props.imageFilePath) }
            >
            </ImageBackground>
        </TouchableOpacity>
    );
}

文件主页

render() {
    let campaigns = [
        {
            id: 1,
            name: "WildFit",
            expire: 1868029014,
            picFrame: "../assets/picture.jpg",
            done: 0.75
        }
    ];

    let campaignViews = campaigns.map( c => {
        return (
            <CampaignPreview
                imageFilePath={ c.picFrame }
                name={ c.name }
                completation={ c.done }
                key={ c.id }
            />              
        );
    });

    let view = (
        <View>
            <ScrollView>
                { campaignViews }
            </ScrollView>
        </View>
        );

    return view;
}

但是如果我用字符串而不是this.props.imageFilePath 就可以了

【问题讨论】:

    标签: image react-native react-props imagesource


    【解决方案1】:

    我似乎在 react native 中无法使用 'require(var)',我一直使用 require('location'),你可以尝试将字符串保存在变量中。

    只需要接受字面量的字符串

    react native use variable for image file

    【讨论】:

      猜你喜欢
      • 2018-10-24
      • 1970-01-01
      • 1970-01-01
      • 2022-07-28
      • 2020-10-02
      • 2021-04-03
      • 2021-01-18
      • 2017-12-23
      相关资源
      最近更新 更多