[英]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.
我尝试了不同的解决方案,使用 useState() 和不使用,但似乎都没有用。 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) Hero.js(父组件)
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) Button.js(子组件)
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;
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:然后像这样在 Button 组件中使用它:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.