官方文档:https://github.com/react-ld/react-wx-images-viewer/blob/master/README-cn.md
1、安装
npm install --save react-wx-images-viewer
2、在项目中使用
1、引入
import WxImageViewer from 'react-wx-images-viewer';
2、设置初始值
this.state = {
viewImgs: [],
viewIndex: 0,
viewOpen: false,
}
3、方法
onClose = () => {
this.setState({
viewOpen: false
})
}
openViewer=(imgs,index)=>{
this.setState({
viewImgs:imgs,
viewIndex: index,
viewOpen: true
})
}
4、渲染
render() {
const { imgs,viewImgs,viewIndex,viewOpen } = this.state;
<div className={styles.comment_item_imgs}>
{
imgs.length > 0 ?
imgs.map((el, index) => {
return (
<div key={index} onClick={this.openViewer.bind(this,imgs,index)} className={styles.item_img_wrap} style={{ backgroundImage: `url(${el})` }}>
</div>
)
}) : '暂无评论图片'
}
</div>
}
5、
{
viewOpen ? <WxImageViewer onClose={this.onClose} urls={this.state.viewImgs} index={viewIndex}/> : ""
}