首页 > 解决方案 > 在渲染之前插入dom元素?

问题描述

我正在使用谷歌地图来显示一个地方。但我在 componentdidmount 阶段更新了地图地址。而且我正在使用另一个函数来创建 dom 元素。所以当我第一次进入地图页面时,我得到错误。因为将插入地图的 dom 元素将在渲染后位于 DOM 内。所以没有刷新我无法对地图页面做出反应。刷新后我可以。知道吗?(我正在使用 MERN)

componentDidMount() {
const {
      match: {
        params: { id }
      }
    } = this.props;

    this.props.getListById(id)
      .then(list => {
        return getLocationAddressAndPlace(
          document.getElementById('__maps'), // This _maps div is not created, will be created after the render.
          list.address.locationName
        )
      })
      .catch(err => {
        toast.error(err);
      });
  }

标签: reactjscomponents

解决方案


推荐阅读