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

116
Visualizações
React.js: How can I download two different files with two different separate Button components?

I have got two buttons as components that need to download two different files, but so far both buttons download the same file that is no surprise for me. I have tried different solutions, with useState() and without, but none of them seem to be working. I know I am missing logic here, but I do not understand where I am missing it. How do I download a different file when clicking the first button, and download another file when clicking on another button? Should there be something that I should recap on, too?

Here is the code:

Hero.js (Parent component)

import Button from "../../components/UI/Button/Button";

const Hero = () => {

  return (
    <div>
      <div>
        <Button>CV (English)</Button>
        <Button>CV (Another Language)</Button>
      </div>
    </div>
  );
};

export default Hero;

Button.js (Child component)

import cv from "../../../assets/files/CV English.pdf";
import anotherLanguage from "../../../assets/files/anotherLanguage.pdf";

const Button = (props) => {
  const file = cv;

  return (
    <button>
      {file === cv ? (
        <a href={cv} download="CV English">
          {props.children}
        </a>
      ) : (
        <a href={anotherLanguage} download="CV Another Language">
          {props.children}
        </a>
      )}
    </button>
  );
};

export default Button;

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

0

You could pass a language prop to the button like this:

      <div>
        <Button language="en">CV (English)</Button>
        <Button>CV (Another Language)</Button>
      </div>

And then use it in the Button component like this:

import cv from "../../../assets/files/CV English.pdf";
import anotherLanguage from "../../../assets/files/anotherLanguage.pdf";

const Button = (props) => {
  const file = cv;

  return (
    <button>
      {props.language === "en" ? (
        <a href={cv} download="CV English">
          {props.children}
        </a>
      ) : (
        <a href={anotherLanguage} download="CV Another Language">
          {props.children}
        </a>
      )}
    </button>
  );
};

export default Button;
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