Зачем реагировать-google-maps, рендеринг одного компонента Circle дважды? - PullRequest
1 голос
/ 02 апреля 2019

Когда я добавил реактив-google-карты в проект, рендер работал дважды.Итак, у меня есть 2 круга один под другим.Также я отображаю координаты центра методом onDragEnd ().Это событие работает только для одного из этих кругов.

Других карт Google в проекте не существует.

Вот несколько способов, которыми я пытался это исправить: 1) Использовать только с GoogleMap, 2) Использовать компонент GoogleMapsWrapper внутри метода render ()родительского компонента, 3) Используйте componentDidMount ();пробуем все из satckoverflow :) и ничего не помогает.

import React, { Component } from 'react';
import MapForm from './mapForm';
import { GoogleMap, withGoogleMap, withScriptjs, Circle } from 'react-google-maps';

const GoogleMapsWrapper = withScriptjs(withGoogleMap(props => {
    const {onMapMounted, ...otherProps} = props;
    return <GoogleMap {...otherProps} ref={c => {
       onMapMounted && onMapMounted(c)
    }}>{props.children}</GoogleMap>
}));

class GoogleMapsContainer extends Component {
    state = {
        coords: {lat:0, lng: 0}
    };


dragCircle = () => {
    this.setState({
        coords: {
            lat: this._circle.getCenter().lat(),
            lng: this._circle.getCenter().lng()
        }
    })
}

render() {
    return(
        <div style={{display: 'flex',flexDirection: 'row', width: '100%', marginLeft: '37px'}}>
        <MapForm 
            filters={this.props.filters}
            coords={this.state.coords}  
        />
        <GoogleMapsWrapper
            googleMapURL={`https://maps.googleapis.com/maps/api/js?key=${KEY}&v=3.exp&libraries=geometry,drawing,places`}
            loadingElement={<div style={{height: `100%`}}/>}
            containerElement={<div style={{position: 'relative',width: '100%',  }} />}
            mapElement={<div style={{height: `100%`}}/>}
            defaultZoom={13}
            defaultCenter={KYIV}
        >
            <Circle
                ref={(circle) => {this._circle = circle}}
                defaultCenter = {KYIV}
                defaultDraggable={true}
                defaultEditable={true}
                defaultRadius={2000}
                onDragEnd = {this.dragCircle}
                options={{
                    strokeColor: `${colors.vividblue}`,
                    fillColor: `${colors.vividblue}`,
                    fillOpacity: 0.1
                }}
            />
      </GoogleMapsWrapper>
      </div>
    )
  }
}

export default GoogleMapsContainer;

Мне нужен только один круг с моими методами. mycircles

1 Ответ

0 голосов
/ 05 апреля 2019

Хорошо, проблема была в компоненте React StrictMode в проекте.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...