[英]React router and props
Need help passing props from different components 需要帮助传递来自不同组件的道具
my routing structure is as follows 我的路由结构如下
app.js app.js
<BrowserRouter>
<div className='App'>
<Switch>
<Route path='/' exact component={Home} />
<Route exact path='/details/:type/:id' component={ItemDetails} />
</Switch>
</div>
</BrowserRouter>
on my Home component, have a bunch of API call's all structured like this 在我的Home组件上,有一堆这样的API调用
getUpcomingMovies = () => {
axios.get('https://api.themoviedb.org/3/movie/upcoming?api_key=40d60badd3d50dea05d2a0e053cc96c3&language=en-US&page=1')
.then((res) => {
console.log(res.data)
this.setState({ upcomingMovies: res.data.results })
})
}
functional component gets rendered like so 功能组件如此呈现
const UpcomingMovies = (props) => {
const upcomingMovieResults = props.upcomingMovies.map(r => (
<Link key={r.id} to={`/details/${r.id}`}>
<div
key={r.id} >
<img src={`https://image.tmdb.org/t/p/w185/${r.poster_path}`} alt={r.title} className='top-movie-results-poster' />
</div>
</Link>
))
return <div className='top-movie-results'>
<h2 className='top-rated-header'>Upcoming Movies</h2>
<div>
<Carousel infinite
slidesPerPage={8}
slidesPerScroll={3}
arrows
animationSpeed={1500}
stopAutoPlayOnHover
offset={50}
itemWidth={225}
clickToChange
centered>{upcomingMovieResults}</Carousel></div>
</div>
}
ItemDetails.js ItemDetails.js
fetchItemDetails = (type = this.props.match.params.type) => {
if (type === 'movie'){
const itemId = this.props.match.params.id;
const ROOT_URL = 'https://api.themoviedb.org/3/movie';
const API_KEY = 'api_key=40d60badd3d50dea05d2a0e053cc96c3&language=en-US';
axios.get(`${ROOT_URL}/${itemId}?${API_KEY}`).then(res => {
console.log(res.data);
console.log(this.props.match.params.type)
this.setState({ itemDetails: res.data })
});
}
};
functional component (child for itemDetails) 功能组件(itemDetails的子代)
const MovieDetails = (props) => {
return <div className='item-details'>
<div>
<a href='#t' className='item-name'>{props.itemDetails.title}</a>
<a href='#t' className='item-name'>{props.itemDetails.name}</a>
</div>
</div>
}
I know this is aa lot of code, but I wanted to give you guys the full spectrum. 我知道这是很多代码,但我想为大家提供完整的信息。
But basically the issue I'm having is when I do 但是基本上我遇到的问题是我什么时候做
<Link key={r.id} to={
/details/${props.itemDetails.type}/${r.id} }>
<Link key={r.id} to={
details/$ <Link key={r.id} to={
props.itemDetails.type}/ }>
<Link key={r.id} to={
r.id} }>
into my functional component, I get 'TypeError: Cannot read property 'type' of undefined', which is on the localhost:3000/ aka home route, but when I manually navigate to localhost:3000/movie/12312 it works fine 到我的功能组件中,我得到“ TypeError:无法读取未定义的属性'type'”,它位于localhost:3000 / aka本地路由上,但是当我手动导航至localhost:3000 / movie / 12312时,它工作正常
so it seems like the issue is that my home route 'localhost:3000/' is not aware of {this.props.type} from itemDetails.. Any ideas? 所以看来问题是我的本地路线“ localhost:3000 /”不了解itemDetails的{this.props.type}。
In functional components, there is no this
or the owner having props. 在功能组件,没有
this
或具有道具所有者。 Use props directly. 直接使用道具。
<Link key={r.id} to={/details/${props.itemDetails.type}/${r.id}}>
try this: 尝试这个:
<Link key={r.id} to={/details/${props.itemDetails && props.itemDetails.type}/${r.id}}>
if this didn't work, you may wanna provide a sandbox demo for this . 如果这不起作用,您可能想为此提供一个沙盒演示 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.