reactjs - Gutenberg - 在渲染 DOM 后调用谷歌地图渲染函数
问题描述
我有点进退两难。
在save
函数renderMap
中,我需要调用一个呈现动态谷歌地图的函数。但是我需要在渲染 DOM 后调用它。我似乎无法为此找到解决方案。我意识到你不能通过save
React 生命周期向函数添加一个类,所以我停止了。它确实适用于该edit
功能。有哪些可能性?
import { __ } from '@wordpress/i18n';
import { registerBlockType } from '@wordpress/blocks';
import { PluginDocumentSettingPanel } from '@wordpress/edit-post';
import { Component } from '@wordpress/element';
const renderMap = function() {
let googleMap = document.getElementById('google-map')
let map
map = new google.maps.Map(googleMap, {
center: { lat: 37.79406, lng: -122.4002 },
zoom: 14,
disableDefaultUI: true,
})
}
registerBlockType( 'splash-blocks/google-maps', {
title: __('Google maps locations', 'google-maps'),
icon: 'megaphone',
category: 'common',
keyword: [
__( 'Display Google maps locations' ),
],
atrributes: {
markers: {
type: 'object'
},
address: {
type: 'string',
default: 'xxxxxxxxx',
},
api_key: {
type: 'string',
default: 'xxxxxxxxx',
}
},
edit: class extends Component {
constructor(props) {
super(props)
}
componentDidMount() {
renderMap()
}
render() {
const { attributes, setAttributes } = this.props
return (
<div id='google-map'>
</div>
)
}
},
save: props => {
const {
className,
attributes: { mapHTML }
} = props;
renderMap()
return (
<div id='google-map'>
</div>
)
}
})
解决方案
推荐阅读
- twitter-bootstrap - 如何添加独立按钮以在引导文件输入上上传所选文件
- java - 由于 RaceCondtion,使用流迭代 LinkedList 正在改变结果列表
- python - 简单的 Peewee ORM Python Bug - 保存钩子
- r - R:如何循环一个多元线性回归,它会降低<2个水平的因子
- php - 如何在php中找到每个子集的总和?
- django - 验证 REST 端点上的字段
- excel - 如何将数据从范围复制到表中的最后一行(转置)?
- spring-boot - 创建多个实体时映射错误
- asp.net - 带有菜单的 ASP.NET 母版页和带有数据输入表单的内容页
- java - 如何在我的代码中添加 try 、 catch 语句以仅接收整数值?