// 初始化state
this.state = {
files: [],
}
// 添加資料到array
this.setState({
files: [
...this.state.files,
fileInfo
]
});
// 動态移除子項從array
removePic(index) {
this.setState({
files: this.state.files.filter((_, i) => i !== index)
})
}
// 循環顯示資料
{state.files.map((file, index) => (
<li key={index}>
<span className="del" onClick={this.removePic.bind(this, index)}>
<a href="javascript:;" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" ><img src="" /></a>
</span>
<span className="p_img">
<a href="javascript:;" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >
<img src={file.src} />
</a>
</span>
</li>
))}
有疑問或技術交流,掃描公衆号一起讨論學習。
更多React線上學習通路:http://each.sinaapp.com/react/index.html