我已經將一個組件分割爲父元素和子元素。我通過狀態將父數據傳遞給孩子,但接收到Uncaught TypeError:無法讀取null的屬性'imageSource',這很奇怪,因爲我遵循與應用程序的另一個元素相同的過程。我也遵循組件和道具文檔,但仍然有點難住。反應:父類和子元素之間的未捕獲類型錯誤
如果我已經定義了狀態,將其設置並添加爲屬性給孩子,狀態如何仍然null?
class Parent extends React.Component {
constructor(){
super();
this.state = {
imageSource: [],
imageTitles: [],
}
}
componentDidMount(){
...
...
// grabbing stuff from Dropbox API
...
...
.then(function(){
that.setState({
imageSource: sources,
imageTitles: titles,
});
});
render(){
return(
<Child imageSource={this.state.imageSource} imageTitles=
{this.state.imageTitles} />
);
}
}
class Child extends React.Component{
render(){
if(!this.state.imageSource.length)
return null;
let titles = this.state.imageTitles.map((el, i) => <p>{el}</p>)
let images = this.state.imageSource.map((el, i) =>
<div className="imageContainer">
<img key={i} className='images' src={el}/>
<div className="imageTitle">{titles[i]}</div>
</div>
)
return (
<div className="ChildWrapper">
{images}
</div>
);
}
}