나는 현재 반작용 배우고, 어떤 물건 초보자를위한 너무 쉽게되지 않습니다 :React : "this.state.x"를 함수로 사용하기 전에 데이터를 대기시키는 법? <p></p> 내가이 (가 <strong>기능 <code>getSlots</code></strong>에 <code>li</code> 배열 덕분에 렌더링 있습니다) <code>renders</code> 간단한 구성 요소를 가지고 ...
render() {
return (
<ul>
{this.getSlots(this.state.viewing).map(item => <li key={item}>{item}</li>)}
</ul>
)
}
getSlots
기능은 :
constructor (props) {...}
getSlots (viewing) {
SOME STUFF...
const monday = this.state.house.monday
return SOME STUFF...
}
componentDidMount() {...}
render() {...}
포인트가있다 getSlots
은 componendDidMount
에 가져올 데이터가 필요합니다. 사실이 시점에서 데이터를 가져 오기 전에 실행되기 때문에 getSlots
이 작동하지 않습니다 (충돌합니다) (실행시 this.state.house.monday
이 "비어 있음").
getSlots
을 실행하기 전에 데이터를 가져올 때까지 기다리는 방법은 무엇입니까? 당신의 단서를 가져 주셔서 감사합니다. 데이터가
getSlots (viewing) {
if (!this.state.house) {
return [];
}
SOME STUFF...
const monday = this.state.house.monday
return SOME STUFF...
}
때 가져온인지 아닌지
'this.state.house'가 예상 한 것과 다른 경우 빈 배열을 반환하십시오. – Bergi