我想找到一个基于它的对象,id
然后将其渲染到组件中。
getInitialState() {
return { item: [] }
},
componentDidMount() {
this.setState({item: this.props.items.filter(item => (item.id == this.props.match.params.id))})
}
componentDidUpdate() {
console.log(this.state.item) // result is empty array
}
如果我不想setState
在render
方法中使用并找到对象,那么我将得到:
render() {
var item = this.props.items.find(item => (item.id == this.props.match.params.id))
console.log(item) // First it is undefined then it will be found
var title = item.title
return (
<div>
{title}
</div>
如何找到基于对象的对象,params.id
然后在组件内部进行渲染?
render() {
var item = this.props.items.find(item => (item.id == this.props.match.params.id))
//This is what you should do to show that it's loading.
if(!item || !item.id){
return <div>Loading</div>
}
console.log(item) // First it is undefined then it will be found
var title = item.title
return (
<div>
{title}
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句