当前位置: 首页 > 知识库问答 >
问题:

失败的道具类型:为“ForwardRef(图像)”提供的道具“源”无效

莫泓
2023-03-14

我试图展示一个简单的例子。通过将图像路径作为道具发送到要渲染它的组件,来创建jpg图像。以下面的方式。

应用程序。js

import React, { Component } from 'react';
import { View, Text, Image } from 'react-native';
import  Header from './components/Header';
import ImageSlider from './components/ImageSlider';
import ImageShow from './components/ImageShow';

class App extends Component {
    render () {
        return (
            <View style={{flex:1}}>
                <Header headerText = "HONEST REVIEWS" />
                <ImageSlider  />
                <ImageShow   imagePath = "./abc.jpg"/>
                <ImageShow   imagePath = "./abc.png" />

            </View>
        );
    }
}

export default App;

图像显示。js

import React from 'react';
import { View, Image, Dimensions } from 'react-native';


const widthOfScreen = Dimensions.get('window').width;

const ImageShow = (imageProps) => {

    return (
        <View>
            <Image  style = { {width: 50, height: 50} } source = { {uri: imageProps.imagePath} } /> 
        </View>

    );

};

const styles = {
    ImageStyle : {
        height: 30,
        width: widthOfScreen
    }
}

export default ImageShow;

图像lider.js

import React from 'react';
import Carousel from 'react-native-banner-carousel';
import { StyleSheet, Image, View, Dimensions } from 'react-native';

const BannerWidth = Dimensions.get('window').width;
const BannerHeight = 250;

const images = [
    require('./abc.jpg'),
    require('./abc.jpg'),
    require('./abc.jpg')
];

export default class ImageSlider extends React.Component {
    renderPage(image, index) {
        return (
            <View key={index}>
                <Image style={styles.imagesInSlider} source = { image } /> 
            </View>
        ); 
    }

    render() {
        return (
            <View style={styles.container}>
                <Carousel
                    autoplay
                    autoplayTimeout={2000}
                    loop
                    index={0}
                    pageSize={BannerWidth}
                >
                    {images.map((image, index) => this.renderPage(image, index))}
                </Carousel>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#fff',
        justifyContent: 'flex-start',
        alignItems: 'center'

    },
    imagesInSlider: {
        width: BannerWidth,
        height: 250,
        resizeMode: 'stretch',
    }
});

我的文件夹结构是:

ProjectName
------src
--------components
-----------ImageShow.js
-----------ImageSlider.js
-----------Header.js
-----------abc.jpg
--------App.js

理想情况下,当我通过本地存储的图像路径时,图像应该被显示,但是我没有看到任何图像显示,而是一条警告消息,上面写着:“失败的道具类型:无效的道具‘源’提供给‘转发参考(图像)’”

共有1个答案

鲜于璞瑜
2023-03-14

这段代码对我来说很有用

import image1 from '../assets/images/abc.png'
import image2 from '../assets/images/abc.png'

const images = [
{
    name: image1
},
{
    name: image2
}]

const RenderPage = ({ image }) => {
 return (
    <Image resizeMode="contain" style={{ width: 50, height: 100, marginBottom: 5 }} source={image} />
 )
}

class myclass extends Component {
 render() {

    return (

        <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
            {images.map((image, index) =>
                <RenderPage key={index} image={image.name} />
            )}
        </View>

    )
 }
}
 类似资料: