简体   繁体   English

反应-componentDidMount之后Google Maps组件不呈现

[英]React - Google Maps component not rendering after componentDidMount

I'm just having trouble with my Google Maps rendering when a search (with a postcode) happens. 发生搜索(带有邮政编码)时,我的Google Maps渲染遇到了麻烦。 It seems like the map is rendering the null map centre, and then not re-rendering after setState in componentDidMount(), as the map is rendered blank/grey but with a hard refresh the map will load with the postcode. 好像地图渲染了空的地图中心,然后在componentDidMount()中的setState之后没有重新渲染,因为地图被渲染为空白/灰色,但是经过硬刷新,地图将加载邮政编码。 I've tried using componentWillMount() without any luck, anyone have any suggestions? 我试过使用componentWillMount()却没有任何运气,有人对它有任何建议吗?

This is the code for my search page: 这是我的搜索页面的代码:

class SearchContainer extends Component {
  constructor(props) {
    super(props);
    this.state = {
      map_centre: null
    };
  }

  componentDidMount() {
    const values = queryString.parse(this.props.location.search);
    axios
      .get("api/v1/search?postcode=" + values.postcode)
      .then(response => {
        var mapCentre = response.data.map_centre;
        this.setState({
          map_centre: mapCentre
        });
      })
      .catch(error => console.log(error));
  }

  render() {
    return (
      <div id="map" className="padding-left">
        <GoogleMapComponent
          townCenters={this.state.townCenters}
          schools={this.state.schools}
          supermarkets={this.state.supermarkets}
          hotels={this.state.hotels}
          airports={this.state.airports}
          trainStations={this.state.trainStations}
          map_centre={this.state.map_centre}
          onMapMounted={this.handleMapMounted}
          onDragEnd={this.handleMapChange}
          onZoomChanged={this.handleMapChange}
          onMarkerClick={this.handleAdspaceShow}
          googleMapURL={url}
          loadingElement={<div style={{ height: `100vh`, width: `100%` }} />}
          containerElement={<div style={{ height: `100vh`, width: `100%` }} />}
          mapElement={<div style={{ height: `100vh`, width: `100%` }} />}
        />
        <img
          src={mapSearch}
          id="map-search-button"
          onClick={this.toggleSearchInput}
        />
        <input
          id="map-search-input"
          className="form-control d-none"
          type="search"
          placeholder="Enter new location"
          aria-label="Search"
          onChange={this.handlePostcodeChange}
          onKeyPress={this.handleNewSearch}
        />
      </div>
    );
  }
}

This is the code for my Google Maps component: 这是我的Google Maps组件的代码:

const GoogleMapComponent = withScriptjs(
  withGoogleMap(props => (
    <GoogleMap
      defaultZoom={13}
      defaultCenter={props.map_centre}
      onDragEnd={props.onDragEnd}
      onZoomChanged={props.onZoomChanged}
      ref={props.onMapMounted}
    />
  ))
);

defaultCenter will only be used by the map for the initial render, so it will not have any effect if you change this value later. map只会将defaultCenter用于初始渲染,因此,如果以后更改此值,它将没有任何效果。

If you use the center prop instead it will work as expected. 如果使用center道具,它将按预期工作。

const GoogleMapComponent = withScriptjs(
  withGoogleMap(props => (
    <GoogleMap
      defaultZoom={13}
      center={props.map_centre}
      onDragEnd={props.onDragEnd}
      onZoomChanged={props.onZoomChanged}
      ref={props.onMapMounted}
    />
  ))
);

Be more specific with the library you are using...you have added the tags "google-maps-react" and "react-google-map" and both are different libraries... 更具体地讲,您正在使用的库...已添加标签“ google-maps-react”和“ react-google-map”,它们都是不同的库...

google-maps-react = https://github.com/fullstackreact/google-maps-react google-maps-react = https://github.com/fullstackreact/google-maps-react

react-google-map = https://tomchentw.github.io/react-google-maps/ react-google-map = https://tomchentw.github.io/react-google-maps/

Anyway, I can see 2 options: 无论如何,我可以看到2个选项:

solution 1: 解决方案1:

Add a conditional that renders GoogleMapComponent only if map_centre is not null. 添加仅当map_centre不为null时才呈现GoogleMapComponent的条件。

  render() {
    if(this.state.map_centre){
      return (
        <div id="map" className="padding-left">
          <GoogleMapComponent
            townCenters={this.state.townCenters}
            schools={this.state.schools}
            supermarkets={this.state.supermarkets}
            hotels={this.state.hotels}
            airports={this.state.airports}
            trainStations={this.state.trainStations}
            map_centre={this.state.map_centre}
            onMapMounted={this.handleMapMounted}
            onDragEnd={this.handleMapChange}
            onZoomChanged={this.handleMapChange}
            onMarkerClick={this.handleAdspaceShow}
            googleMapURL={url}
            loadingElement={<div style={{ height: `100vh`, width: `100%` }} />}
            containerElement={<div style={{ height: `100vh`, width: `100%` }} />}
            mapElement={<div style={{ height: `100vh`, width: `100%` }} />}
           />
          <img
          src={mapSearch}
          id="map-search-button"
          onClick={this.toggleSearchInput}
          />
          <input
          id="map-search-input"
          className="form-control d-none"
          type="search"
          placeholder="Enter new location"
          aria-label="Search"
          onChange={this.handlePostcodeChange}
          onKeyPress={this.handleNewSearch}
          />
      </div>);
     }
     return <div> Loading.. </div>;
  }

solution 2: 解决方案2:

Set a default lat/lng (not null) before you get the response. 设置默认纬度/经度(不为null),然后再获取响应。

constructor(props) {
    super(props);
    this.state = {
      map_centre: { lat: -34.397, lng: 150.644 }
    };
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应:componentDidMount + setState 不重新渲染组件 - React: componentDidMount + setState not re-rendering the component 使用来自 componentDidMount React 中的 fetch 属性的渲染组件 - Rendering component with property coming from fetch in componentDidMount React React Native组件未使用componentWillMount或componentDidMount中的异步redux操作呈现 - React Native component not rendering with async redux action in componentWillMount or componentDidMount Google Maps React / Redux组件 - Google Maps React/Redux component componentDidMount在React Native中持续渲染 - componentDidMount is rendering continuously in React Native 更改componentDidMount中的状态后,React组件不会更新 - React component do not gets updated after changing the the state in componentDidMount 在转换后渲染组件 - React - Rendering a component after a transition - React 渲染另一个组件的渲染器 function 中声明的组件不包括 REACT 17 中 componentDidMount 方法的更改 - Rendering a component declared inside the render function of another component does not include changes in the componentDidMount method in REACT 17 将道具传递给React组件中的componentDidMount() - Pass props to componentDidMount() in React component 在组件重新加载时未调用 React componentDidMount - React componentDidMount not called on component reload
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM