nandorojo / react-native-anchors

🦅 Anchor links and scroll-to utilities for React Native (+ Web)
MIT License
295 stars 3 forks source link

I want to get the anchor I'm currently scrolling to, what should I do? #7

Closed FrontEndNoah closed 10 months ago

FrontEndNoah commented 10 months ago

I have tried to calculate using measure, but not work on ts:

  const color = React.useRef<View>(null);
  const userScroll = React.useRef(false);
  const onAnchorsScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
    if (userScroll.current) {
      console.log(event.nativeEvent.contentOffset.y);
      color.current?.measure((x, y, w, h, px, py) => {
        console.log('color', y, h, py);
      });
    }
  };
```"
on render
    <ScrollView
      anchors={anchors}
      onScroll={onAnchorsScroll}
      onTouchStart={() => (userScroll.current = true)}
      onTouchEnd={() => (userScroll.current = false)}>
    // some node
      <Target ref={color} name="Color">
        <HomeColor />
      </Target>
    <ScrollView>
but i got `'color', undefined, undefined, undefined` on console
and i had try to wrap target with view
      <View
        style={[
          {height: 200},
        ]}
        ref={color}>
        <Target name="color">
          <HomeColor />
        </Target>
      </View>

but still got `'color', undefined, undefined, undefined`
FrontEndNoah commented 10 months ago

I found a solution, code like this

<Target name="color">
  <View
    style={[
     // at least one style needs to be set, some work(like backgroundColor: '#0000'), some not work(like height: 'auto')
    ]}
    ref={color}>
      <HomeColor />
  </View>
</Target>

but I still hope to get measure directly through Target...

nandorojo commented 10 months ago

measure isn’t the right function. you can check the source code to see how it works.

FrontEndNoah commented 10 months ago

measure isn’t the right function. you can check the source code to see how it works.

I checked the documentation and it seems to be the reason for Collapsable