【问题标题】:React-google-maps re-rendering issueReact-google-maps 重新渲染问题
【发布时间】:2016-03-24 14:29:18
【问题描述】:

我在使用 react-google-maps npm 模块时遇到了一些问题。

第一次渲染,效果很好。 重新渲染地图组件时,我得到了经典的 google-maps 错误。

Uncaught TypeError: Cannot read property 'offsetWidth' of null
Uncaught (in promise) TypeError: Cannot read property 'componentWillReceiveProps' of null(…)

因为渲染时地图对象不可用?

我的 google-maps 地图组件

import React, { PropTypes } from 'react'
import { GoogleMap, Marker, GoogleMapLoader } from 'react-google-maps'
export class Map extends React.Component {
  static propTypes = {
    coordinates: PropTypes.object,
    markers: PropTypes.array
  };

  render () {
    return (<section onloadstyle={{height: '300px'}}>
      <GoogleMapLoader
        containerElement={
          <div
            {...this.props}
            style={{
              height: '300px',
              width: '100%'
            }}
          />
        }
        googleMapElement={
          <GoogleMap
            ref={(map) => console.log(map)}
            defaultZoom={15}
            defaultCenter={this.props.coordinates}>
            {this.props.markers.map((marker, index) => {
              return (
                <Marker key={index} {...marker} />
              )
            })}
          </GoogleMap>
        }
      />
    </section>)
  }
}

export default Map

我使用这样的组件:

var coordinates = {lat: this.props.item.delivery_address_lat, lng: this.props.item.delivery_address_lng}
var map = this.props.item.delivery_address_lat !== 0 && this.props.item.delivery_address_lng !== 0 ? (<Row id='map'>
      <Map markers={[{position: coordinates}]} coordinates={coordinates} />
</Row>) : ''

这是因为 google-maps-react 模块没有正确卸载组件还是我做了什么?

下面是head里面的

<script src="https://maps.googleapis.com/maps/api/js?key=MY-KEY"></script>

编辑

试图以非 react-redux 的方式解决它,但这绝不是一个解决方案,因为它会产生错误消息:Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the Map component.

但是,地图仍然正确地重新渲染。我尝试通过调用传递的 prop 函数和状态的 redux 方式来执行此操作,并将 {map: section} 设置为状态,将其从调用视图传递下来。没有解决任何问题并导致相同的错误消息,即使它被 setTimeout 延迟了

我有点卡在这里,不知道如何解决这个问题。

componentDidMount () {
    this.setState({map: null})
    setTimeout(() => this.setState({
      map: <section onloadstyle={{height: '300px'}}>
      <GoogleMapLoader
        containerElement={
          <div
            {...this.props}
            style={{
              height: '300px',
              width: '100%'
            }}
          />
        }
        googleMapElement={
          <GoogleMap
            ref={(map) => console.log(map)}
            defaultZoom={15}
            defaultCenter={this.props.coordinates}>
            {this.props.markers.map((marker, index) => {
              return (
                <Marker key={index} {...marker} />
              )
            })}
          </GoogleMap>
        }
      />
    </section>
    }), 300)
  }

  render () {
    if (!this.state) {
      return <span />
    } else {
      return this.state.map
    }
  }

【问题讨论】:

    标签: google-maps reactjs redux


    【解决方案1】:

    第二次编辑的解决方案是清除componentWillUnmount()函数中的setTimeout()调用。

    总是必须在卸载组件时清除间隔和超时。

    componentDidMount () {
        this.setState({map: null})
        this.timeout = setTimeout(() => this.setState({
          map: <section onloadstyle={{height: '300px'}}>
          <GoogleMapLoader
            containerElement={
              <div
                {...this.props}
                style={{
                  height: '300px',
                  width: '100%'
                }}
              />
            }
            googleMapElement={
              <GoogleMap
                ref={(map) => console.log(map)}
                defaultZoom={15}
                defaultCenter={this.props.coordinates}>
                {this.props.markers.map((marker, index) => {
                  return (
                    <Marker key={index} {...marker} />
                  )
                })}
              </GoogleMap>
            }
          />
        </section>
        }), 300)
      }
    
      componentWillUnmount () {
        clearTimeout(this.timeout)
      }
    
      render () {
        if (!this.state) {
          return <span />
        } else {
          return this.state.map
        }
      }
    

    这个解决方案不是一个好的解决方案,并且不符合 react-redux 工作流程。但它有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-21
      • 2022-11-27
      • 2021-12-13
      • 2020-08-18
      • 2023-04-06
      相关资源
      最近更新 更多