在反应路由器 dom v6 中渲染
render in react router dom v6
我正在关注如何呈现函数和传递属性的示例,以从 dom.
获取一些数据
--- App.jsx ---
import { Routes, Route } from "react-router-dom";
// Components
import Listado from "./components/Listado";
import Detalle from "./components/Detalle";
function App() {
const addOrRemoveFromFavs = e => {
const btn = e.currentTarget; // console.log(btn);
const parent = btn.parentElement; // console.log(parent)
const imgURL = parent.querySelector('img').getAttribute('src');
const title = parent.querySelector('h5').innerText;
const overview = parent.querySelector('p').innerText;
const movieData = {
imgURL, title, overview
}
console.log(movieData);
}
return (
<>
<Header />
<div className="container mt-3">
<Routes>
<Route path="/" element={<Login />} />
<Route path="/listado" render={ (props) => <Lisstado addOrRemoveFromFavs={addOrRemoveFromFavs} {...props}/>} />
<Route path="/detalle" element={<Detalle />} />
</Routes>
</div>
<Footer />
</>
);
}
根据react v6中的文档,渲染器或组件不再使用,所以我不知道如何传递信息,这里我展示了另一部分代码,我想分享功能和来自这里从DOM获取数据。按照这个例子制作道具的控制台时,应该会得到一个带有信息的对象,但它不是那样的,而且它停止加载信息,现在是空白。
-- listado.jsx ---
import { useEffect, useState } from "react";
import { Link, Navigate } from "react-router-dom";
import axios from "axios";
import swAlert from "@sweetalert/with-react";
const Listado = (props) => {
let token = sessionStorage.getItem('token');
console.log(props);
onst [ moviesList, setMoviesList ] = useState([]);
useEffect(() => {
const endpoint = 'https://api.themoviedb.org/3/discover/movie?api_key=8764e6417ae516fb86bc820ee865a6d8&language=es-US&page=1'
axios.get(endpoint)
.then(response => {
const apiData = response.data;
setMoviesList(apiData.results);
})
.catch(error => {
swAlert(<h2>Hubo errores, intenta más tarde</h2>)
})
}, [setMoviesList]);
return (
<>
{ !token && <Navigate to="/" /> }
<div className="row">
{
moviesList.map((oneMovie, index) => {
return (
<div className="col-3" key={index}>
<div className="card my-4">
<img src={ `https://image.tmdb.org/t/p/w500/${oneMovie.poster_path}` } className="card-img-top" alt="..." />
<button className="favourite-btn">
</button>
<div className="card-body">
<h5 className="card-title">{ oneMovie.title.substring(0, 20) }...</h5>
<p className="card-text">{ oneMovie.overview.substring(0, 100) }...</p>
<Link to={`/detalle?peliculitaID=${oneMovie.id}`} className="btn btn-primary">View Details</Link>
</div>
</div>
</div>
)
})
}
</div>
</>
)
}
export default Listado
感谢任何建议
如果您只是想在 "/listado"
路径上渲染 Lisstado
组件并传递一个 addOrRemoveFromFavs
道具,您可以像其他任何时候将道具传递给一个组件。请记住 Route
组件采用 ReactNode
、a.k.a。 JSX,在 element
道具上,所以传递道具与 React 中的其他任何地方一样。
示例:
<Routes>
<Route path="/" element={<Login />} />
<Route
path="/listado"
element={<Lisstado addOrRemoveFromFavs={addOrRemoveFromFavs} />}
/>
<Route path="/detalle" element={<Detalle />} />
</Routes>
此外,您不应该在 DOM 中查询传递的回调中的值,而是将它们直接传递给子组件中的回调。
示例:
应用程序
const addOrRemoveFromFavs = movieData => e => {
console.log(movieData);
}
列表
const Listado = ({ addOrRemoveFromFavs }) => {
const token = sessionStorage.getItem('token');
const [ moviesList, setMoviesList ] = useState([]);
useEffect(() => {
...
}, [setMoviesList]);
if (!token) {
return <Navigate to="/" />;
}
return (
<div className="row">
{moviesList.map((oneMovie, index) => {
// create movieData object
const movieData = {
imgURL: `https://image.tmdb.org/t/p/w500/${oneMovie.poster_path}`,
overview: oneMovie.overview,
title: oneMovie.title
};
return (
<div className="col-3" key={index}>
<div className="card my-4">
<img
src={ `https://image.tmdb.org/t/p/w500/${oneMovie.poster_path}`}
className="card-img-top"
alt="..."
/>
<button
className="favourite-btn"
onClick={addOrRemoveFromFavs(movieData)} // <-- pass to callback
>
</button>
<div className="card-body">
<h5 className="card-title">
{oneMovie.title.substring(0, 20) }...
</h5>
<p className="card-text">
{oneMovie.overview.substring(0, 100) }...
</p>
<Link
to={`/detalle?peliculitaID=${oneMovie.id}`}
className="btn btn-primary"
>
View Details
</Link>
</div>
</div>
</div>
)}
)}
</div>
)
};
我正在关注如何呈现函数和传递属性的示例,以从 dom.
获取一些数据--- App.jsx ---
import { Routes, Route } from "react-router-dom";
// Components
import Listado from "./components/Listado";
import Detalle from "./components/Detalle";
function App() {
const addOrRemoveFromFavs = e => {
const btn = e.currentTarget; // console.log(btn);
const parent = btn.parentElement; // console.log(parent)
const imgURL = parent.querySelector('img').getAttribute('src');
const title = parent.querySelector('h5').innerText;
const overview = parent.querySelector('p').innerText;
const movieData = {
imgURL, title, overview
}
console.log(movieData);
}
return (
<>
<Header />
<div className="container mt-3">
<Routes>
<Route path="/" element={<Login />} />
<Route path="/listado" render={ (props) => <Lisstado addOrRemoveFromFavs={addOrRemoveFromFavs} {...props}/>} />
<Route path="/detalle" element={<Detalle />} />
</Routes>
</div>
<Footer />
</>
);
}
根据react v6中的文档,渲染器或组件不再使用,所以我不知道如何传递信息,这里我展示了另一部分代码,我想分享功能和来自这里从DOM获取数据。按照这个例子制作道具的控制台时,应该会得到一个带有信息的对象,但它不是那样的,而且它停止加载信息,现在是空白。
-- listado.jsx ---
import { useEffect, useState } from "react";
import { Link, Navigate } from "react-router-dom";
import axios from "axios";
import swAlert from "@sweetalert/with-react";
const Listado = (props) => {
let token = sessionStorage.getItem('token');
console.log(props);
onst [ moviesList, setMoviesList ] = useState([]);
useEffect(() => {
const endpoint = 'https://api.themoviedb.org/3/discover/movie?api_key=8764e6417ae516fb86bc820ee865a6d8&language=es-US&page=1'
axios.get(endpoint)
.then(response => {
const apiData = response.data;
setMoviesList(apiData.results);
})
.catch(error => {
swAlert(<h2>Hubo errores, intenta más tarde</h2>)
})
}, [setMoviesList]);
return (
<>
{ !token && <Navigate to="/" /> }
<div className="row">
{
moviesList.map((oneMovie, index) => {
return (
<div className="col-3" key={index}>
<div className="card my-4">
<img src={ `https://image.tmdb.org/t/p/w500/${oneMovie.poster_path}` } className="card-img-top" alt="..." />
<button className="favourite-btn">
</button>
<div className="card-body">
<h5 className="card-title">{ oneMovie.title.substring(0, 20) }...</h5>
<p className="card-text">{ oneMovie.overview.substring(0, 100) }...</p>
<Link to={`/detalle?peliculitaID=${oneMovie.id}`} className="btn btn-primary">View Details</Link>
</div>
</div>
</div>
)
})
}
</div>
</>
)
}
export default Listado
感谢任何建议
如果您只是想在 "/listado"
路径上渲染 Lisstado
组件并传递一个 addOrRemoveFromFavs
道具,您可以像其他任何时候将道具传递给一个组件。请记住 Route
组件采用 ReactNode
、a.k.a。 JSX,在 element
道具上,所以传递道具与 React 中的其他任何地方一样。
示例:
<Routes>
<Route path="/" element={<Login />} />
<Route
path="/listado"
element={<Lisstado addOrRemoveFromFavs={addOrRemoveFromFavs} />}
/>
<Route path="/detalle" element={<Detalle />} />
</Routes>
此外,您不应该在 DOM 中查询传递的回调中的值,而是将它们直接传递给子组件中的回调。
示例:
应用程序
const addOrRemoveFromFavs = movieData => e => {
console.log(movieData);
}
列表
const Listado = ({ addOrRemoveFromFavs }) => {
const token = sessionStorage.getItem('token');
const [ moviesList, setMoviesList ] = useState([]);
useEffect(() => {
...
}, [setMoviesList]);
if (!token) {
return <Navigate to="/" />;
}
return (
<div className="row">
{moviesList.map((oneMovie, index) => {
// create movieData object
const movieData = {
imgURL: `https://image.tmdb.org/t/p/w500/${oneMovie.poster_path}`,
overview: oneMovie.overview,
title: oneMovie.title
};
return (
<div className="col-3" key={index}>
<div className="card my-4">
<img
src={ `https://image.tmdb.org/t/p/w500/${oneMovie.poster_path}`}
className="card-img-top"
alt="..."
/>
<button
className="favourite-btn"
onClick={addOrRemoveFromFavs(movieData)} // <-- pass to callback
>
</button>
<div className="card-body">
<h5 className="card-title">
{oneMovie.title.substring(0, 20) }...
</h5>
<p className="card-text">
{oneMovie.overview.substring(0, 100) }...
</p>
<Link
to={`/detalle?peliculitaID=${oneMovie.id}`}
className="btn btn-primary"
>
View Details
</Link>
</div>
</div>
</div>
)}
)}
</div>
)
};