我有几张自助卡.单击它们后,我将重定向到另一个页面,在那里我使用通过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--&gt;",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--&gt;",Image1Url);

但是imgTag不能渲染图像.为什么会发生这种情况?

One Observation :

如果我从App.js直接拨打contryInfo.jsx,那么我就可以看到图像.img标记正确渲染图像

function App() {
  return (
    <BrowserRouter>
      <CountryInfo />
      <Pages />
    </BrowserRouter>
  );
}

推荐答案

图像源路径使用relative路径,因为它们不以"/"字符开头,例如它们是当前URL路径的源relative.当您在应用程序的根目录中呈现CountryInfo时,(assumed)路径只是"/",因此PUBLIC目录中的相对路径工作,例如"/public/images/new_0.jpg",但是当在"/country/:countryId"上呈现CountryInfo时,图像是相对于路径this(例如/public/country/0/images/new_0.jpg")来提供的.

将数据更新为使用absolute路径:

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",
    },
  },
  ...
];

或在渲染时将其附加到图像源字符串:

function CountryInfo() {
  const location = useLocation();
  const countryId = location.state?.data;

  if (countryData) {
    const image1Url = countryData[countryId].images.image1;
    return (
      <div className="">
        <img
          src={`/${image1Url}`}
          alt="country"
          style={{ height: "100vh", width: "100vw" }}
        />
      </div>
    );
  ...

另请注意:由于导航时URL路径中已经使用了country.id,因此在路由状态中传递相同的country.id值实际上没有意义.只需使用路径段来获取传递的国家/地区ID.将路由绘制CountryInfo定义为具有动态路径段,例如path="/country/:countryId".

示例:

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/country/:countryId" element={<CountryInfo />} />
        <Route path="/" element={<Pages />} />
      </Routes>
    </BrowserRouter>
  );
}
<div className="row mt-3">
  {firstRowData.map((country) => (
    <div className="col-md-3" key={country.id}>
      <NavLink to={`/country/${country.id}`}>
        { ...Card code... }
      </NavLink>
    </div>
  ))}
</div>
import { useParams } from 'react-router-dom';

function CountryInfo() {
  const countryId = useParams();

  const { image1, name } = countryData[countryId]?.images ?? {};

  if (image1) {
    return (
      <div className="">
        <img
          src={image1}
          alt={name}
          style={{ height: "100vh", width: "100vw" }}
        />
      </div>
    );
  ...

Javascript相关问答推荐

React Hooks中useState的同步问题

从mat—country—select获取整个Country数组

如何从JSON数组添加Google Maps标记—或者如何为数组添加参数到标记构造器

显示图—如何在图例项上添加删除线效果?

如何从网站www.example.com获取表与Cheerio谷歌应用程序脚本

如何将react—flanet map添加到remixjs应用程序

DOM不自动更新,尽管运行倒计时TS,JS

面对代码中的错误作为前端与后端的集成

按下单键和多值

在D3条形图中对具有相同X值的多条记录进行分组

FileReader()不能处理Firefox和GiB文件

将相关数据组合到两个不同的数组中

react -原生向量-图标笔划宽度

AG-GRIDreact 显示布尔值而不是复选框

select 2-删除js插入的项目将其保留为选项

未找到用于 Select 器的元素:in( puppeteer 师错误)

bootstrap S JS赢得了REACT中的函数/加载

Reaction-在同一页内滚动导航(下拉菜单)

带元素数组的Mongo聚合

如何从嵌套的json中获取最大值