简体   繁体   English

访问ref react native onpress

[英]access to ref react native onpress

I'am using to display a list of view and with this a list of marker. 我用来显示视图列表以及标记列表。 I want to link my marker with my Animated.scrollview but using ref it's not working (i may use ref in a wrong way) 我想将标记与Animated.scrollview链接,但是使用ref无效(我可能以错误的方式使用ref)

there is my work 有我的工作

return (
  <View style={styles.container}>
    <MapView
      ref={map => this.map = map}
      initialRegion={this.state.region}
      style={styles.container}
    >
      <UrlTile
        urlTemplate="http://IP:8080/styles/klokantech-basic/{z}/{x}/{y}.png"
        zIndex={-1}
      />
      {this.state.markers.map((marker, index) => {

        return (
          <MapView.Marker key={index} coordinate={marker.coordinates} onPress={() => this.myRef.scrollTo({x: 0, y: 0})
        } >
            <Animated.View style={[styles.markerWrap, opacityStyle]} >
              <Animated.View style={[styles.ring, scaleStyle]} />
              <View style={styles.marker} />
            </Animated.View>
          </MapView.Marker>
        );

      })}
    </MapView>
    <Animated.ScrollView
      horizontal
      ref={c => (this.myRef = c)}
      scrollEventThrottle={1}
      showsHorizontalScrollIndicator={true}
      snapToInterval={CARD_WIDTH}
      onScroll={Animated.event(
        [{
          nativeEvent: {
            contentOffset: {
              x: this.animation,
            },
          },
        },],
        { useNativeDriver: true }
      )}
      style={styles.scrollView}
      contentContainerStyle={styles.endPadding}
    >
      {this.state.markers.map((marker, index) => {
          return (
            <View style={styles.card} key={index}>
              <Image
                source={marker.image}
                style={styles.cardImage}
                resizeMode="cover"
              />
              <View style={styles.textContent}>
                <Text numberOfLines={1} style={styles.cardtitle}>{marker.espace.titre}</Text>
                <Text numberOfLines={1} style={styles.cardDescription}>
                  {marker.description}
                </Text>
              </View>
            </View>)
      })
      }
    </Animated.ScrollView>
  </View>
);

when i try call this.myRef.scrollTo({x: 0, y: 0} i get _this4.myRef.scrollTo is not a function 当我尝试调用this.myRef.scrollTo({x:0,y:0}时,我得到_this4.myRef.scrollTo不是函数

i think you need replace refs, should working: 我认为您需要更换裁判,应该工作:

// MapView
ref={map => {if (map) this.map=map}}

// Animated.ScrollView
ref={c => {if (c) this.myRef=c}}

UPDATE 更新

// there is typo in your code
// Animated.ScrollView
ref={c => (this.myRef = c)}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM