栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在render方法中使用承诺来渲染React组件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在render方法中使用承诺来渲染React组件

render()
方法应该从渲染UI
this.props
this.state
,所以异步加载数据,则可以使用
this.state
存储
imageId: imageUrl
的映射。

然后,在您的

componentDidMount()
方法中,您可以
imageUrl
从填充
imageId
。然后,
render()
通过渲染
this.state
对象,
该方法应该是纯粹而简单的

请注意,

this.state.imageUrls
异步填充了,因此渲染的图像列表项在获取其网址后将一一显示。您还可以
this.state.imageUrls
使用所有图片ID或索引(不带网址)初始化,这样您可以在加载图片时显示加载器。

constructor(props) {    super(props)    this.state = {        imageUrls: []    };}componentDidMount() {    this.props.items.map((item) => {        ImageStore.getImageById(item.imageId).then(image => { const mapping = {id: item.imageId, url: image.url}; const newUrls = this.state.imageUrls.slice(); newUrls.push(mapping); this.setState({ imageUrls: newUrls });        })    });}render() {    return (      <div>        {this.state.imageUrls.map(mapping => ( <div>id: {mapping.id}, url: {mapping.url}</div>        ))}      </div>    );}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/387511.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号