【发布时间】:2019-10-14 12:25:54
【问题描述】:
需要帮助将状态作为道具传递给另一个状态组件。我对 React 很陌生,我不确定我做错了什么。当我在 Timer 组件中使用 console.log 时,它显示未定义,但是当我在 Main 组件中使用 console.log 时,它完美地显示了对象。
class Main extends React.Component {
constructor() {
super()
this.state = {
isLoaded: false,
itemsP:{}
}
}
componentDidMount() {
fetch("https://api.spacexdata.com/v3/launches/next")
.then(response => response.json())
.then(
(resData) =>
this.setState({
isLoaded: true,
itemsP: resData
})
)
}
render() {
console.log(this.state.itemsP) //this will console.log the object from the api
return (
<main>
<Timer nextLaunch={this.state.itemsP} />
</main>
)
}
}
//Timer component
class Timer extends React.Component{
constructor(props) {
super(props)
this.state = {
nextDate: props.nextLaunch.launch_date_utc
}
}
render() {
console.log(this.state.nextDate) //will console log UNDEFINED why is this?
return (
<div>
//display something....
</div>
)
}
}
here 是我用作参考的 API 的链接。
【问题讨论】:
-
你可以显示你的
Timer组件
标签: javascript reactjs