模态不显示在本地反应?

问题描述 投票:2回答:2

嘿伙计们我试图在点击地图标记显示模态,但我没有看到标记点击任何人可以帮助我出问题。我正在使用来自https://github.com/maxs15/react-native-modalbox的Modal

这是代码:

 import Modal from "react-native-modalbox";

export default class App {
openModal4(id) {
    this.refs.modal4.open();
}
render() {
    return (
        <View style={style.mainContainer}>
            <MapView
                ref="map"
                showUserLocation={true}
                region={this.state.region}
                onRegionChange={this.onRegionChange}
                onRegionChangeComplete={this.onRegionChangeComplete}
                style={styles.map}
                rotateEnabled={false}
                showsCompass={false}
            >
                {this.state.markers.map(function(marker) {
                    return (
                        <MapView.Marker
                            coordinate={marker.latlng}
                            key={marker.id}
                            onPress={this.openModal4}
                        />
                    );
                })}
            </MapView>
            <View
                style={{
                    position: "absolute",
                    width: windowsWidth,
                    height: windowsHeight - 100,
                    alignItems: "center",
                    justifyContent: "center"
                }}
            >
                <Image source={require("./assets/map-marker.png")} />
            </View>
            <Modal
                style={[styles.modal, styles.modal4]}
                position={"bottom"}
                ref={"modal4"}
            />
        </View>
    );
}
}

任何人都可以帮助我吗?提前致谢。

view modal-dialog react-native marker
2个回答
0
投票

您应该进行更改,在回调中绑定它,请阅读本文Don't Use Bind When Passing Props

openModal4 = (id) => {
  this.refs.modal4.open();
}

0
投票

Guys Binding是使用arrow函数后解决的问题=>这里是代码

openModal4(id){
    this.refs.modal4.open();
  } 

{this.state.markers.map((marker) => {
            return(
              <MapView.Marker coordinate={marker.latlng} key={marker.id} onPress={this.openModal4} />
            );
          })}

你需要在构造函数中绑定函数,因为它包含'this'

this.openModal4 = this.openModal4.bind(this)

希望我的斗争会帮助别人

© www.soinside.com 2019 - 2024. All rights reserved.