React Native:如何在每次状态更新时停止重新渲染地图标记

时间:2019-05-14 22:05:44

标签: reactjs react-native react-native-ios react-native-maps

我有一个组件,该组件的地图具有针对各个位置的多个自定义标记,而轮播带有这些相同位置的卡。用户按下标记时,它应该显示标注,并在标记旁边(但在标注外部)显示位置名称。

但是,由于我在onRegionChangeComplete中更新了状态,因此如果用户移动了地图,然后快速按下了标记(在状态完成从调用setState中的onRegionChangeComplete进行更新之前),那么标记将在触发onPress事件之前重新渲染,并且永远不会触发该事件。

一种解决方案可能是使用shouldComponentUpdate,但是文档指出,该解决方案仅应用于性能优化,而不能防止重新渲染(https://reactjs.org/docs/react-component.html#shouldcomponentupdate),但更重要的是,我的{ {1}}函数具有一些取决于componentDidUpdate中设置的区域的条件逻辑以及其他条件操作,因此我不想阻止整个组件的重新渲染,而只是防止不必要的重新渲染标记。

我还使用了https://github.com/react-native-community/react-native-maps/issues/2082中提到的性能优化,即将制造商包装在实现shouldComponentUpdateshouldComponentUpdate的组件中,但是,我不确定这是否正在做任何事情都是因为父组件似乎只是重新创建了所有优化的标记,而不是使用它们的优化来处理重新渲染。另外,即使我不使用包装标记,而使用常规的自定义标记,我仍然遇到相同的问题。

我也已经在react-native-maps上为此打开了一个问题,但是还没有得到回应:https://github.com/react-native-community/react-native-maps/issues/2860

我的“ onRegionComplete”函数在移动地图时更新状态。为了简洁起见,我删除了其他一些条件状态更新:

getDerivedStateFromProps

使用更常规的标记(不是经过优化的版本)的MapView:

onRegionChangeComplete = (region) => {
    const nextState = { };
    nextState.region = region;

    if (this.state.showNoResultsCard) {
      nextState.showNoResultsCard = false;
    }

    .
    .
    .

    this.setState({ ...nextState });

    this.props.setSearchRect({
      latitude1: region.latitude + (region.latitudeDelta / 2),
      longitude1: region.longitude + (region.longitudeDelta / 2),
      latitude2: region.latitude - (region.latitudeDelta / 2),
      longitude2: region.longitude - (region.longitudeDelta / 2)
    });
  }

我的标记事件活动功能:

<MapView // show if loaded or show a message asking for location
    provider={PROVIDER_GOOGLE}
    style={{ flex: 1, minHeight: 200, minWidth: 200 }}
    initialRegion={constants.initialRegion}
    ref={this.mapRef}
    onRegionChange={this.onRegionChange}
    onRegionChangeComplete={this.onRegionChangeComplete}
    showsUserLocationButton={false}
    showsPointsOfInterest={false}
    showsCompass={false}
    moveOnMarkerPress={false}
    onMapReady={this.onMapReady}
    customMapStyle={mapStyle}
    zoomTapEnabled={false}
    >

        {this.state.isMapReady && this.props.places.map((place, index) => {
            const calloutText = this.getDealText(place, 'callout');
            return (
                <Marker
                    tracksViewChanges
                    key={Shortid.generate()}
                    ref={(ref) => { this.markers[index] = ref; }}
                    coordinate={{
                        latitude: place.getLatitude(),
                        longitude: place.getLongitude()
                    }}
                    onPress={() => { this.onMarkerSelect(index); }}
                    anchor={{ x: 0.05, y: 0.9 }}
                    centerOffset={{ x: 400, y: -60 }}
                    calloutOffset={{ x: 8, y: 0 }}
                    calloutAnchor={{ x: 0.075, y: 0 }}
                    image={require('../../Assets/icons8-marker-80.png')}
                    style={index === this.state.scrollIndex ? { zIndex: 2 } : null}
                >
               {this.state.scrollIndex === index &&
                    <Text style={styles.markerTitle}>{place.getName()}</Text>}

                  <Callout onPress={() => this.onCalloutTap(place)} tooltip={false}>
                    <View style={{
                      borderColor: red,
                      width: 240,
                      borderWidth: 0,
                      borderRadius: 20,
                      paddingHorizontal: 8,
                      flexDirection: 'column',
                      justifyContent: 'flex-start',
                      alignItems: 'center'
                    }}
                    >
                      <Text style={styles.Title}>Now:</Text>
                      <View style={{
                        width: 240,
                        flexDirection: 'column',
                        justifyContent: 'space-evenly',
                        alignItems: 'flex-start',
                        paddingHorizontal: 8,
                        flex: 1
                      }}
                      >
                        {calloutText.Text}
                    </View>
                </View>
            </Callout>
        </Marker>
        );
    }) 
}

</MapView>

更新状态,然后快速按下标记将导致不会触发onPress事件。同样,每次更新父组件时,标记都会重新渲染/重新创建。 (我之所以说是重新创建,是因为标记似乎在重新渲染而没有触发onMarkerSelect(index) { this.setState({ scrollIndex: index }); this.carousel._component.scrollToIndex({ index, animated: true, viewOffset: 0, viewPosition: 0.5 }); this.markers[index].redrawCallout(); } e或shouldComponentUpdat。)

是否有任何方法可以更新componentDidUpdate中的状态而无需强制重新标记?

1 个答案:

答案 0 :(得分:1)

对于碰巧遇到此问题的其他人,问题在于我是随机生成标记的密钥,从而导致父组件在每次重新渲染时都创建新的标记。

具体来说,key={Shortid.generate()}行就是问题所在。

相关问题