Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

549
Visualizações
react.js rendering dynamic images not working

I want to render an image in my 'card' component, which gets the image url from the props, but the image won't show. I have created the react app with npx create-react-app, the image is in the src/images and the card component is in src/components. the card component:

import React from 'react'

export default function Card(props) {
    return (
        <section className='card'>
        <img/>
        <div className='card--info'>
            <img src={props.img}/>
            <p className='card--info'>
                <span className='gray'>{props.rating}</span> ({props.reviewCount}) • {props.country}
            </p>
            <p>{props.title}</p>
            <p><span className='bold'>{props.price}</span> / person</p>
        </div>
        </section>
    )    
}

the app component:

import React from "react"
import Navbar from './components/Navbar'
import Hero from './components/Hero'
import Card from './components/Card'

export default function App(){
    return (
        <div>
            <Navbar />
            <Hero />
            <section className="cards">
                <Card img = '../images/image12.png'
                    rating="5.0"
                    reviewCount = {6}
                    country = "USA"
                    title = "Life lessons with Katie Zaferes"
                    price = {136} />
            </section>
        </div>
    )
}

I have also tried the followin methods: <img src={require(props.img)}/> -> this one will show an error: Uncaught Error: Cannot find module '../images/image12.png' <img src={new URL(props.img, import.meta.url)

does anyone else know what should I do?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to import the image to use if you have a static image.

import React from "react"
import Navbar from './components/Navbar'
import Hero from './components/Hero'
import Card from './components/Card'
import defImage from '../images/image12.png'

export default function App(){
    return (
        <div>
            <Navbar />
            <Hero />
            <section className="cards">
                <Card img ={defImage}
                    rating="5.0"
                    reviewCount = {6}
                    country = "USA"
                    title = "Life lessons with Katie Zaferes"
                    price = {136} />
            </section>
        </div>
    )
}

Also If you have a dynamic image name and you want to use that. Try using it this way.

import React from "react"
import Navbar from './components/Navbar'
import Hero from './components/Hero'
import Card from './components/Card'
export default function App(){
    return (
        <div>
            <Navbar />
            <Hero />
            <section className="cards">
                <Card img ="image12.png"
                    rating="5.0"
                    reviewCount = {6}
                    country = "USA"
                    title = "Life lessons with Katie Zaferes"
                    price = {136} />
            </section>
        </div>
    )
}

And in card component:

import React from 'react'

export default function Card(props) {
    return (
        <section className='card'>
        <img/>
        <div className='card--info'>
            <img {require(`../images/${props.img}`)}/>
            <p className='card--info'>
                <span className='gray'>{props.rating}</span> ({props.reviewCount}) • {props.country}
            </p>
            <p>{props.title}</p>
            <p><span className='bold'>{props.price}</span> / person</p>
        </div>
        </section>
    )    
}

Make sure if you are using images with require then those images should be in public folder

about 4 years ago · Juan Pablo Isaza Relatório

0

Meybe a path is not correct? Put image to public folder and then use /image/name.png I use it all the time. I think if I render something for everybody, so it can be on public.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda