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

Mar*_*lus 5 reactjs react-native react-native-ios react-native-maps

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

但是,因为我更新的状态onRegionChangeComplete,如果用户移动地图,然后迅速按下标记(状态完成从调用更新前setStateonRegionChangeComplete烧制前),那么标记将重新渲染onPress事件,而该事件从来没有发射。

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

我还使用中提到的性能优化https://github.com/react-native-community/react-native-maps/issues/2082在实现组件包裹厂商shouldComponentUpdategetDerivedStateFromProps然而,我不完全确定这可以做任何事情,因为父组件似乎只是在重新创建我所有的优化标记,而不是使用其优化来处理重新渲染。而且,即使我不使用包装标记,而使用常规的自定义标记,我仍然遇到相同的问题。

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

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

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)
    });
  }
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

我的功能是针对标记的新闻事件:

onMarkerSelect(index) {
    this.setState({ scrollIndex: index });
    this.carousel._component.scrollToIndex({
      index,
      animated: true,
      viewOffset: 0,
      viewPosition: 0.5
    });

    this.markers[index].redrawCallout();
}
Run Code Online (Sandbox Code Playgroud)

更新状态,然后快速按下标记将导致不会触发onPress事件。同样,每次更新父组件时,标记都会重新渲染/重新创建。(我之所以说重新创建,是因为标记似乎在重新渲染而没有触发shouldComponentUpdate或componentDidUpdate)。

有什么方法可以在onRegionChangeComplete不强制重新渲染标记的情况下更新状态?

Mar*_*lus 3

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

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