Reactjs ReactRouter-无法将道具或参数传递给;链接至;组件
我是一个新的反应和尝试做一些非常简单的事情。我正在访问父组件中的API。我想访问每个对象的ID,然后将其传递给子组件。我已经设法获得了要在URI中显示的ID,因此/offers/:ID。我试过两件事:Reactjs ReactRouter-无法将道具或参数传递给;链接至;组件,reactjs,react-router-v4,react-props,react-component,Reactjs,React Router V4,React Props,React Component,我是一个新的反应和尝试做一些非常简单的事情。我正在访问父组件中的API。我想访问每个对象的ID,然后将其传递给子组件。我已经设法获得了要在URI中显示的ID,因此/offers/:ID。我试过两件事: 在“Link to”中传递ID,然后使用“props.location.state”或“this.props.location.state”从我的组件进行访问,这样做不起作用 通过参数“props.match.params”从URI访问ID无效 谁能告诉我我做错了什么 App.js: export
export default class App extends Component {
render() {
return (
<div>
<Switch>
<Route exact path='/offers' component={ OffersContainer }/>
<Route path='/offers/:id' component={ OfferContainer} />
</Switch>
</div>
);
}
}
它总是返回到子组件中相同的错误“未定义道具”,因为它是类组件,所以您必须使用此属性。道具:
componentDidMount() {
const { id } = this.props.match.params
console.log(id)
fetch(`/api/offers/${id}`)
....
}
在子组件中,由于它是类组件,因此必须使用
this.props
:
componentDidMount() {
const { id } = this.props.match.params
console.log(id)
fetch(`/api/offers/${id}`)
....
}
componentDidMount() {
const { id } = this.props.match.params
console.log(id)
fetch(`/api/offers/${id}`)
....
}