React Router 在组件之间共享数据
React Router sharing data between components
我在我的 React 应用程序中设置了一条动态路线,当用户单击图像时,它会导航到一条新路线 url /details/:id
:
<div className='App'>
<Switch>
<Route path='/' exact component={Home} />
<Route exact path='/details/:id' component={ItemDetails} />
</Switch>
</div>
它来自我的功能组件:
const headerImages = (props) => {
const imageResults = props.trending.slice(0, 5).map(r => ( // Grab firt 5 array objects
<Link key={r.id} to={`/details/${r.id}`}>
<div key={r.id}>
<img key={r.id} src={`https://image.tmdb.org/t/p/w1280${r.backdrop_path}`} alt={r.title} className='header-image' />
<h1 className='now-playing'>Latest</h1>
<h1 className='header-title'>{r.title}</h1>
<h4 className='header-details'>{r.overview}</h4>
</div>
</Link>
))
return <div className='header-images'>
<Carousel infinite autoPlay={4500}>{imageResults}</Carousel>
</div>
}
export default headerImages
ItemDetails
是一个基于 class 的组件,它有一个 API 调用,如何从我的功能组件中获取 r.id 值到我的 Api打电话?
class ItemDetails extends Component {
constructor (props) {
super(props)
this.state = { selectedItem: null }
}
fetchItemDetails = () => {
axios.get('https://api.themoviedb.org/3/movie/${this.props.r.id}?api_key=40d60badd3d50dea05d2a0e053cc96c3&language=en-US')
.then((res) => {
console.log(res.data.results)
})
}
componentDidMount(){
this.fetchItemDetails()
}
render () {
return <h1>test</h1>
}
}
目前 API 调用 returns undefined
,但如您所见,我正在尝试将动态 ID 传递到调用中。
更新的解决方案:
class ItemDetails extends Component {
constructor (props) {
super(props)
this.fetchItemDetails = this.fetchItemDetails.bind(this)
}
fetchItemDetails = (itemId = this.props.match.params.id) => {
axios.get('https://api.themoviedb.org/3/movie/${itemId}?api_key=40d60badd3d50dea05d2a0e053cc96c3&language=en-US')
.then((res) => {
console.log(res.data.results)
})
}
componentDidMount(){
this.fetchItemDetails()
}
render () {
return <h1>test</h1>
}
}
export default ItemDetails
您可以使用:
const id = this.props.match.params.id
获取 id 并从该特定 id 获取数据。
这是因为您没有将 this 绑定到试图访问 props 的函数。
在构造函数中添加:
this.fetchItemDetails = this.fetchItemDetails.bind(this);
和 url 应该使用模板文字 ` 而不是引号 '
`https://api.themoviedb.org/3/movie/${itemId}?api_key=40d60badd3d50dea05d2a0e053cc96c3&language=en-US`
试试这个:
import React, { Component } from 'react';
import axios from 'axios';
export default class ItemDtails extends Component {
fetchItemDetails = () => {
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.results);
});
};
componentDidMount() {
this.fetchItemDetails()
}
render() {
return <div>Test...</div>;
}
}
我在我的 React 应用程序中设置了一条动态路线,当用户单击图像时,它会导航到一条新路线 url /details/:id
:
<div className='App'>
<Switch>
<Route path='/' exact component={Home} />
<Route exact path='/details/:id' component={ItemDetails} />
</Switch>
</div>
它来自我的功能组件:
const headerImages = (props) => {
const imageResults = props.trending.slice(0, 5).map(r => ( // Grab firt 5 array objects
<Link key={r.id} to={`/details/${r.id}`}>
<div key={r.id}>
<img key={r.id} src={`https://image.tmdb.org/t/p/w1280${r.backdrop_path}`} alt={r.title} className='header-image' />
<h1 className='now-playing'>Latest</h1>
<h1 className='header-title'>{r.title}</h1>
<h4 className='header-details'>{r.overview}</h4>
</div>
</Link>
))
return <div className='header-images'>
<Carousel infinite autoPlay={4500}>{imageResults}</Carousel>
</div>
}
export default headerImages
ItemDetails
是一个基于 class 的组件,它有一个 API 调用,如何从我的功能组件中获取 r.id 值到我的 Api打电话?
class ItemDetails extends Component {
constructor (props) {
super(props)
this.state = { selectedItem: null }
}
fetchItemDetails = () => {
axios.get('https://api.themoviedb.org/3/movie/${this.props.r.id}?api_key=40d60badd3d50dea05d2a0e053cc96c3&language=en-US')
.then((res) => {
console.log(res.data.results)
})
}
componentDidMount(){
this.fetchItemDetails()
}
render () {
return <h1>test</h1>
}
}
目前 API 调用 returns undefined
,但如您所见,我正在尝试将动态 ID 传递到调用中。
更新的解决方案:
class ItemDetails extends Component {
constructor (props) {
super(props)
this.fetchItemDetails = this.fetchItemDetails.bind(this)
}
fetchItemDetails = (itemId = this.props.match.params.id) => {
axios.get('https://api.themoviedb.org/3/movie/${itemId}?api_key=40d60badd3d50dea05d2a0e053cc96c3&language=en-US')
.then((res) => {
console.log(res.data.results)
})
}
componentDidMount(){
this.fetchItemDetails()
}
render () {
return <h1>test</h1>
}
}
export default ItemDetails
您可以使用: const id = this.props.match.params.id
获取 id 并从该特定 id 获取数据。
这是因为您没有将 this 绑定到试图访问 props 的函数。
在构造函数中添加:
this.fetchItemDetails = this.fetchItemDetails.bind(this);
和 url 应该使用模板文字 ` 而不是引号 '
`https://api.themoviedb.org/3/movie/${itemId}?api_key=40d60badd3d50dea05d2a0e053cc96c3&language=en-US`
试试这个:
import React, { Component } from 'react';
import axios from 'axios';
export default class ItemDtails extends Component {
fetchItemDetails = () => {
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.results);
});
};
componentDidMount() {
this.fetchItemDetails()
}
render() {
return <div>Test...</div>;
}
}