0

I am creating a map screen with markers and animated bottom cards. Now what I am trying to do is, when I click on a marker point,I want to open the cards list and show the relevant card at the center. With my code, on my first click I got this error "Cannot read property 'scrollTo' of null," but when I dismiss it on emulator, It start perfectly. Just on first tap I am getting this error. Please Help me. error screen on first marker click this is: after I dismiss the error

//Here is my onpress function

 const _scrollView = React.useRef(null);

const onMarkerPress = (e) => {
    const markerID = e._targetInst.return.key;

    let x = (markerID * CARD_WIDTH) + (markerID * 20)-30; 
console.log(x)
    _scrollView.current.scrollTo({x: x, y: 0, animated: true});
  }

// Markers render code
{ChargingPoints.map((marker, index) => (
          <Marker

            onPress={(e)=> {setCardVisible(true)
              onMarkerPress(e)
            }}
            onDeselect={()=> setCardVisible(false)}
            resize
            key={index}
            coordinate={{
              latitude: marker.coordinate.latitude,
              longitude: marker.coordinate.longitude,
            }}
            title={marker.name}
          >
            <Image
              source={charging_station_icon}
              style={{ width: 35, height: 35 }}
            />
          </Marker>
        ))}

 //CARD RENDER CODE
 
 {isCardVisible? <Animated.ScrollView 
      ref={_scrollView}
        scrollEnabled
        horizontal
        scrollEventThrottle={1}
        showsHorizontalScrollIndicator={false}
        style={styles.scrollView}
        pagingEnabled
        snapToInterval={CARD_WIDTH + 20}
        snapToAlignment="center"
        contentInset={{
          top: 0,
          left:SPACING_FOR_CARD_INSET,
          bottom: 0,
          right: SPACING_FOR_CARD_INSET,
        }}
       
        onScroll={Animated.event(
          [
            {
              nativeEvent: {
                contentOffset: {
                  x: mapAnimation,
                },
              },
            },
          ],
          { useNativeDriver: true }
        )}
      >
        {ChargingPoints.map((marker, index) => (
          <View style={styles.card} key={index}>
            <View style={styles.textContent}>
              <Text numberOfLines={1} style={styles.cardtitle}>
                {marker.name}
              </Text>
              <Text numberOfLines={1} style={styles.cardDescription}>
                Total number of Charging Points: {marker.TotalChargingPoints}
              </Text>
              <Text numberOfLines={1} style={styles.cardDescription}>
                Number of available Charging Points:
                {marker.AvailableChargingPoints}
              </Text>
              <Text numberOfLines={1} style={styles.cardDescription}>
                Density:
                {(
                  marker.UnavaialbleChargingPoints / marker.TotalChargingPoints
                ).toPrecision(2) * 100}
                %
              </Text>
            </View>
          </View>
        ))}
      </Animated.ScrollView>:<></> }
MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
KreaN
  • 3
  • 3
  • use useEffect hook and call `onMarkerPress(e)` inside it when cardVisible is changed and set to true. The useState hook updates the value in next render of component. Refer https://stackoverflow.com/questions/55983047/strange-behavior-of-react-hooks-delayed-data-update – Ravi Yadav Aug 04 '23 at 13:01
  • @RaviYadav onMarkerPress(e) function is used in array.map function and it tooks specific "e" value so I am not able to use it on useEffect. On the other hand I dont have problem with scrolling between cards after first marker press so there must be another solution I guess – KreaN Aug 08 '23 at 08:18

0 Answers0