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

125
Visualizações
React Image Rendering using require()

I'm trying to render images in react using require(). Below is a snapshot of the code that is working.

const ProjectBlock: React.FC<{ data: PROJECTBLOCK }> = (props) => {
  const baseUrl = "../../../assets/images/forkify.png";
  return (
    <div className="projectblock">
      <div className="projectblock__heading">
        <h3 className="subheading-default margin-bottom">{props.data.heading}</h3>
        <p className="para">{props.data.content}</p>
        <h4 className="smallheadin">{props.data.technologies.join(" · ")}</h4>
      </div>
      <img
        src={require("../../../assets/images/forkify.png")}
        alt=""
        className="projectblock__image"
      />
    </div>
  );
};

In the above case, I'm passing a string to the require() function. However when I try and store the string in the baseUrl variable and pass it to the require() function, it does not work and gives the following error in the console.

Uncaught Error: Cannot find module '../../../assets/images/forkify.png'

Here is how I am trying to use the variable with require()

const ProjectBlock: React.FC<{ data: PROJECTBLOCK }> = (props) => {
  const baseUrl = "../../../assets/images/forkify.png";
  return (
    <div className="projectblock">
      <div className="projectblock__heading">
        <h3 className="subheading-default margin-bottom">{props.data.heading}</h3>
        <p className="para">{props.data.content}</p>
        <h4 className="smallheadin">{props.data.technologies.join(" · ")}</h4>
      </div>
      <img src={require(baseUrl)} alt="" className="projectblock__image" />
    </div>
  );
};

Have even tried with template literals. Please help!

about 4 years ago · Juan Pablo Isaza
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