React-router 组件没有路由到我想要的地方
我正在用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来
Movie
组件正在使用相对路线路径,并且从主页上与在其中一种类型路由上进行错误构建它们。使用绝对路径,即带有领先
“/”
字符的路径,并构建包含类型
id的路径。示例:
<<> < /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 thegenre
id.Example: