我有几张自助卡.单击它们后,我将重定向到另一个页面,在那里我使用通过NavLink传递的数据并呈现它.
这是我的代码.
Destination.jsx
<div className="row mt-3">
{firstRowData.map((country, index) => (
<div className="col-md-3" key={index}>
<NavLink
to={`/country/${country.id}`}
state={{ data: country.id }}
>
{ Card code }
</NavLink>
</div>
))}
</div>
CountryInfo.jsx
import React from "react";
import { useLocation} from "react-router-dom";
import countryData from "./data";
function CountryInfo() {
const location = useLocation();
let countryId = location.state?.data;
if (countryData) {
const image1Url = countryData[countryId].images.image1;
Console.log("Image1-->;",Image1Url);
return (
<div className="">
<img
src={image1Url}
alt=""
style={{ height: "100vh", width: "100vw" }}
/>
</div>
);
我有一个包含json数据的data.jsx文件.
const data = [
{
id: 0,
name: "Singapore",
url: "images/card_singapore.jpg",
price: "$ 300",
day: 5,
images: {
image1: "images/new_0.jpg",
image2: "images/new_1.jpg",
image3: "images/new_2.jpg",
image4: "images/new_3.jpg",
},
},
// some more data.....
通过下面的代码,我能够打印出正确的图像地址.
Console.log("Image1-->;",Image1Url);
但是img
Tag不能渲染图像.为什么会发生这种情况?
One Observation :个
如果我从App.js
直接拨打contryInfo.jsx
,那么我就可以看到图像.img
标记正确渲染图像
function App() {
return (
<BrowserRouter>
<CountryInfo />
<Pages />
</BrowserRouter>
);
}