reactjs 在React JS中单击项目时导航到子文件夹中的页面

omtl5h9j  于 2022-12-03  发布在  React
关注(0)|答案(3)|浏览(130)

我对React JS很陌生,但看了一些关于如何建立我自己的投资组合的视频教程。我想Map我的投资组合项目,其中包含图像,标题,描述,角色和链接。我设法Map了所有内容,除了链接到各自的投资组合项目的详细页面。
有没有人能帮助我了解我做错了什么,以及如何解决它?
这是我的文件夹结构:
src => pages(保留主页面)和src => pages => case-studies(保留我的投资组合项目的详细页面)。

以下是我到目前为止的进展:

在src =〉pages文件夹中使用.jsx

import React, { useEffect, useState } from "react";
import Loader from "./Loader";
import "./Work.css";
import WorkCard from "./WorkCard";
import WorkData from "./WorkData";

const Work = () => {
  return (
    <div className="work">
      <section className="work-section">
        <div className="card">
          <h1 className="underline">My portfolio</h1>
          <p>Here are the case studies of my projects...</p>

          <div className="grid-layout grid-2">
            {
              WorkData.map((val, index) => {
                return (
                  <WorkCard
                    key={index}
                    url={val.url}
                    image={val.image}
                    name={val.name}
                    description={val.description}
                    role={val.role} />
                 );
              })
            }
          </div>
        </div>
      </section>
    </div>
  );
}

export default Work;

工作卡.jsx在src =〉pages文件夹中

import React from "react";
import { NavLink } from "react-router-dom";

const WorkCard = (props) => {
  return (
    <NavLink to={props.url} className="tile-card">
      <img src={props.image} alt={props.name} />
      <div className="details">
        <h2>{props.name}</h2>
        <p className="description" title={props.description}>{props.description}</p>
        <h4 className="role" title={props.role}>Role: {props.role}</h4>
      </div>
    </NavLink>
  );
}

export default WorkCard;

工作数据.jsx位于src =〉pages文件夹中

import EcoPizza from "../assets/project-icons/ecoPizza.webp";
import Squared from "../assets/project-icons/Squared.webp";
import { EcoPizzaCaseStudy, SquaredCaseStudy } from "./case-studies";

const WorkData = [
  {
    "image": EcoPizza,
    "name": "The UX portfolio item name",
    "description": "This is description part",
    "role": "My role in that project",
    "url": EcoPizzaCaseStudy
  },
  {
    "image": Squared,
    "name": "The UX portfolio item name",
    "description": "This is description part",
    "role": "My role in that project",
    "url": SquaredCaseStudy
  }
];

export default WorkData;

index.js位于src =〉pages =〉案例研究文件夹中

export { default as EcoPizzaCaseStudy } from "./eco-pizza/EcoPizzaCaseStudy";
export { default as SquaredCaseStudy } from "./squared/SquaredCaseStudy";

应用程序js

import React from "react";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import { Navigation } from "./components/Navigation";
import { Home, About, Work, Contact, PageNotFound, UnderConstruction } from "./pages";
import { Footer } from "./components/Footer";

function App() {
  return (
    <div className="App">
      <Router>
        <Navigation />
        <main>
          <Routes>
            <Route path="/" element={<Home />} />
            <Route path="/about" element={<About />} />
            <Route path="/work" element={<Work />} />
            <Route path="/under-construction" element={<UnderConstruction />} />
            <Route path="/contact" element={<Contact />} />
            <Route path='*' element={<PageNotFound />}/>
          </Routes>
        </main>
        <Footer />
      </Router>
    </div>
  );
}

export default App;
xdyibdwo

xdyibdwo1#

你错过了整个路由的概念。你不能只是使用项目作为链接到它。为此,我们使用路由,然后使用URL的路由定义相应。
因此,您需要在App.js中用缺少的项目填充您的Routes:

import React from "react";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import { Navigation } from "./components/Navigation";
import { Home, About, Work, Contact, PageNotFound, UnderConstruction } from "./pages";
import { Footer } from "./components/Footer";
//import required components
import { EcoPizzaCaseStudy, SquaredCaseStudy } from "./case-studies";

function App() {
  return (
    <div className="App">
      <Router>
        <Navigation />
        <main>
          <Routes>
            <Route path="/" element={<Home />} />
            <Route path="/about" element={<About />} />
            <Route path="/work" element={<Work />} />
            <Route path="/under-construction" element={<UnderConstruction />} />
            <Route path="/contact" element={<Contact />} />
            // Add your routes
            <Route path="/eco-pizza-case-study" element={<EcoPizzaCaseStudy/>} />
            <Route path="/squared-case-study" element={<SquaredCaseStudy/>} />
            <Route path='*' element={<PageNotFound />}/>
          </Routes>
        </main>
        <Footer />
      </Router>
    </div>
  );
}

export default App;

然后对url使用该定义路径。
而不是"url": SquaredCaseStudy只做"url": "/squared-case-study"
您选择的任何路径只要是unique就可以使用。您可以将其设置为/study/squared或任何其他路径,但您需要在App.js中定义它才能使用

k7fdbhmy

k7fdbhmy2#

我认为你需要先了解React Router的概念,以检查如何在组件之间导航。React使用React Router Dom来导航页面。所以首先你需要在你的项目中创建所有的路由(命名为App.jsx,或者你可以创建你自己的自定义Route.jsx文件并导入到App.jsx)

import React, { Suspense, lazy } from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';

const Work = lazy(() => import('./pages/Work'));
const EcoPizzaCaseStudy = lazy(() => import('./case-studies/eco-pizza/EcoPizzaCaseStudy'));
const SquaredCaseStudy = lazy(() => import('./case-studies/squared/SquaredCaseStudy'));

const App = () => (
  <Router>
    <Suspense fallback={<div>Loading...</div>}>
      <Routes>
        <Route path="/work" element={<Home />} />
        <Route path="/coPizzaCaseStudy/" element={<EcoPizzaCaseStudy />} />
        <Route path="/squaredCaseStudy/" element={<SquaredCaseStudy />} />
      </Routes>
    </Suspense>
  </Router>
);

通过这种方式,您可以将urls coPizzaCaseStudysquaredCaseStudy传递给您的workData,而不是传递组件本身,您不需要在WorkData.jsx文件中导入组件,只需在url属性中使用上述文本即可。
而且在我个人的想法关于你的项目结构,你可以使子文件夹内的网页文件夹的每一页相关的文件(例如,创建Work文件夹,并将您的文件放在Work.css ~ WorkData.jsx),而不是使用Work.jsx,您可以只拥有index.jsx并从Work. jsx复制所有代码这样可以帮助您webpack编译引擎为您页面找到正确索引(在这个例子中,工作页面)更容易。我分享了我的项目文件夹结构遵循最佳实践。子文件夹下的视图是每一页。每个文件夹有索引.tsx文件的主要组件,和组件子文件夹,挂钩,和类型等

如果你只想使用对象数据而不是JSX组件,你还需要命名为WorkData.jsx

rqdpfwrv

rqdpfwrv3#

因此,如果我对您的问题的理解是正确的话,例如,您单击了链接,然后导航到"/work/EcoPizzaCaseStudy",然后呈现了PageNotFound组件,或者出现了一些错误。
代码当前正在尝试将导入的React组件***用作***链接目标。

import EcoPizza from "../assets/project-icons/ecoPizza.webp";
import Squared from "../assets/project-icons/Squared.webp";
import { EcoPizzaCaseStudy, SquaredCaseStudy } from "./case-studies";

const WorkData = [
  {
    "image": EcoPizza,
    "name": "The UX portfolio item name",
    "description": "This is description part",
    "role": "My role in that project",
    "url": EcoPizzaCaseStudy // <-- React component, not a path string
  },
  {
    "image": Squared,
    "name": "The UX portfolio item name",
    "description": "This is description part",
    "role": "My role in that project",
    "url": SquaredCaseStudy // <-- React component, not a path string
  }
];

导入的组件应由Route组件呈现,并且链接应指定目标路径字符串。
您需要为要链接到的工作项目显式渲染路线。
示例:
更新工作数据以包括要在特定路由上呈现的元素,然后设置要在其上呈现的URL路径。

import EcoPizza from "../assets/project-icons/ecoPizza.webp";
import Squared from "../assets/project-icons/Squared.webp";
import { EcoPizzaCaseStudy, SquaredCaseStudy } from "./case-studies";

const WorkData = [
  {
    image: EcoPizza,
    name: "The UX portfolio item name",
    description: "This is description part",
    role: "My role in that project",
    element: <EcoPizzaCaseStudy />, // <-- route element
    path: "eco-pizza", // <-- route path
  },
  {
    image: Squared,
    name: "The UX portfolio item name",
    description: "This is description part",
    role: "My role in that project",
    element: <SquaredCaseStudy />,
    path: "squared",
  }
];

export default WorkData;

WorkCard组件将使用path"/work"路径进行相对导航,即导航到"/work/eco-pizza"
第一次
WorkData数组Map到项目路由。

...
import WorkData from "./WorkData";
...

function App() {
  return (
    <div className="App">
      <Router>
        <Navigation />
        <main>
          <Routes>
            <Route path="/" element={<Home />} />
            <Route path="/about" element={<About />} />
            <Route path="/work">
              <Route index element={<Work />} />
              {/* "/work/eco-pizza", "/work/squared", etc... */}
              {WorkData.map(({ path, element }) => (
                <Route key={path} {...{ path, element }} />
              ))}
            </Route>
            <Route path="/under-construction" element={<UnderConstruction />} />
            <Route path="/contact" element={<Contact />} />
            <Route path='*' element={<PageNotFound />}/>
          </Routes>
        </main>
        <Footer />
      </Router>
    </div>
  );
}

相关问题