reactjs - react-map-gl 警告 onViewportChange
问题描述
我正在测试版本为“4.0.0-beta.3”( https://github.com/uber/react-map-gl )的库示例,但我对方法 onViewportChange 有此警告:
警告:在现有状态转换期间无法更新(例如在渲染中)。渲染方法应该是 props 和 state 的纯函数。
解决方案
我在 4.1.13 得到这个。
onViewportChange
您可以通过仅在安装组件后响应来解决警告:
class Map extends Component {
state = {
viewport: {
width: 400,
height: 400,
latitude: -33.9249,
longitude: 18.4241,
zoom: 8
},
mounted: false
}
componentDidMount () {
this.setState({ mounted: true })
}
render () {
const { mounted } = this.state
return (
<ReactMapGL
mapboxApiAccessToken={<token>}
{...this.state.viewport}
onViewportChange={(viewport) => {
if (mounted) { this.setState({ viewport }) }
}}
/>
)
}
}
推荐阅读
- asp.net-web-api - 如何在 MessageHandler 中查看 WebAPI 函数的属性?
- css - 粘性列创建堆叠的堆叠上下文
- go - JSON 对象未解组,但未引发错误
- python - 移植 theano function() 并更新到 Pytorch(负采样 RuntimeError: Expected hidden size)
- css - 如何在不同的屏幕尺寸上保持容器的比例
- php - 数据表搜索返回 Sql Exceloption
- angular - 角度测试找不到打开的对话框
- rust - 不可变 Vec 的 Arc/Mutex 的可变展开
- jitsi - 如何在 JitsiMeet 中设置状态记录模式
- javascript - discord.js 无法读取未定义的属性“第一个”