React-router 组件没有路由到我想要的地方

发布于 2025-01-19 09:28:17 字数 2620 浏览 0 评论 0原文

我正在用 React 制作一个电影应用程序。

因此,我正在获取 Genre 中的流派列表。基于流派 ID,我正在创建一条新路线来呈现 MoviesFromGenre 中的电影列表。使用电影的 ID,当我单击 Movie 组件时,我将创建一条到 MovieDetails 组件的新路由。这部分有效。

当我从主页单击 Movie 组件时,问题就出现了。我正在 MoviesFromType 中获取热门电影列表,但是当我单击 Movie 组件时,路由路径不起作用。我希望它转到 MovieDetails 路径 ( /:genreId/:movieId ),而不是似乎转到 /:movieId它会显示 MoviesFromGenre 中的内容。

因此,如果我从主页单击《蜘蛛侠无路回家》,它不会将我重定向到该电影页面的详细信息。但点击“动作”类型,然后点击“蜘蛛侠无路回家”会将我重定向到详细信息页面。

沙盒 链接

App.js

import "./styles.css";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Navbar from "./Layouts/Navbar/Navbar";
import Layouts from "./Pages/Layouts.js";
import MoviesFromGenre from "./Components/MoviesFromGenre/MoviesFromGenre";
import MovieDetails from "./Components/MovieDetails/MovieDetails";

function App() {
  return (
    <div className="App">
      <Router>
        <Navbar />
        <Routes>
          <Route path="/*" element={<Layouts />} />
          <Route path=":genreId/" element={<MoviesFromGenre />} />
          <Route path=":genreId/:movieId" element={<MovieDetails />} />
        </Routes>
      </Router>
    </div>
  );
}

export default App;

MoviesFromType.js

import React, {useState} from 'react'
import { useParams } from 'react-router-dom'
import useFetch from '../Utils/useFetch'
import '../Utils/useFetch'
import { API_KEY, API_URL} from '../../api/requests'
import Movie from '../Movie/Movie'

function MoviesFromGenre () {
  const BASE_URL = `${API_URL}movie/popular?api_key=${API_KEY}`
  const { data: results, loading, error } = useFetch(BASE_URL, {results: []})
  console.log(results)
  return (
    <div className='movie-list-type'>
      popular
      {
        loading ?
         'Loading' :
        results.results.map(result => (
          <Movie key={result.id} result = {result} />
        ))
      }
      {error ? error : null}
    </div>
  )
}

export default MoviesFromGenre;

电影.js

import React from 'react'
import { Link } from 'react-router-dom'

function Movie (props) {
  const {result} = props;
  return (
    <div className='movie'>
      <Link to={`${result.id}`}>{result.title}</Link>
    </div>
  )
}

export default Movie;

I'm making a movie app in React.

So I'm fetching a list of genres in Genre. Based on the genre id I'm creating a new route to render a list of movies in MoviesFromGenre. Using the movie's id, when I click on a Movie component I'm making a new route to a MovieDetails component. This part works.

The problem comes when I click on a Movie component from the homepage. I'm fetching a list of popular movies in MoviesFromType, but when I click on a Movie component the routing path doesn't work. I would like for it to go to the MovieDetails path ( /:genreId/:movieId ), instead it seems to go to /:movieId and it displays what's in MoviesFromGenre.

So, If I click on Spider-Man No Way home from the homepage, it won't redirect me to that movies page details. But clicking on the Action genre and then on Spider-Man No Way Home redirects me to the details page.

Sandbox Link

App.js

import "./styles.css";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Navbar from "./Layouts/Navbar/Navbar";
import Layouts from "./Pages/Layouts.js";
import MoviesFromGenre from "./Components/MoviesFromGenre/MoviesFromGenre";
import MovieDetails from "./Components/MovieDetails/MovieDetails";

function App() {
  return (
    <div className="App">
      <Router>
        <Navbar />
        <Routes>
          <Route path="/*" element={<Layouts />} />
          <Route path=":genreId/" element={<MoviesFromGenre />} />
          <Route path=":genreId/:movieId" element={<MovieDetails />} />
        </Routes>
      </Router>
    </div>
  );
}

export default App;

MoviesFromType.js

import React, {useState} from 'react'
import { useParams } from 'react-router-dom'
import useFetch from '../Utils/useFetch'
import '../Utils/useFetch'
import { API_KEY, API_URL} from '../../api/requests'
import Movie from '../Movie/Movie'

function MoviesFromGenre () {
  const BASE_URL = `${API_URL}movie/popular?api_key=${API_KEY}`
  const { data: results, loading, error } = useFetch(BASE_URL, {results: []})
  console.log(results)
  return (
    <div className='movie-list-type'>
      popular
      {
        loading ?
         'Loading' :
        results.results.map(result => (
          <Movie key={result.id} result = {result} />
        ))
      }
      {error ? error : null}
    </div>
  )
}

export default MoviesFromGenre;

Movie.js

import React from 'react'
import { Link } from 'react-router-dom'

function Movie (props) {
  const {result} = props;
  return (
    <div className='movie'>
      <Link to={`${result.id}`}>{result.title}</Link>
    </div>
  )
}

export default Movie;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ花落谁相伴 2025-01-26 09:28:17

看来Movie组件正在使用相对路线路径,并且从主页上与在其中一种类型路由上进行错误构建它们。

使用绝对路径,即带有领先“/”字符的路径,并构建包含类型 id的路径。

示例:

function Movie(props) {
  const { result } = props;
  return (
    <div className="movie">
      <Link to={`/${result.genre_ids[0]}/${result.id}`}>{result.title}</Link>
    </div>
  );
}

“ <<> < /a>

Seems the Movie component is using relative route paths and incorrectly building them from the home page versus when on one of the genre routes.

Use an absolute path, i.e. a path with a leading "/" character, and build a path that includes the genre id.

Example:

function Movie(props) {
  const { result } = props;
  return (
    <div className="movie">
      <Link to={`/${result.genre_ids[0]}/${result.id}`}>{result.title}</Link>
    </div>
  );
}

Edit react-router-component-not-routing-where-i-want

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文