I'm currently learning React, and some stuff are not so easy for a newbie...
I have a simple component which renders
this (note that it renders a li
array thanks to function getSlots
):
render () {
return (
<ul>
{this.getSlots(this.state.viewing).map(item => <li key={item}>{item}</li>)}
</ul>
)
}
Function getSlots
is:
constructor (props) {...}
getSlots (viewing) {
SOME STUFF...
const monday = this.state.house.monday
return SOME STUFF...
}
componentDidMount () {...}
render () {...}
The point is that getSlots
needs data to be fetched in componendDidMount
to work. Indeed, at this time, getSlots
doesn't work (it crashes) because it runs before data are fetched (this.state.house.monday
is "empty" when it runs).
How do I wait for data to be fetched before running getSlots
? Thanks for your clue.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…