2.1.2 • Published 1 year ago

react-native-appear-observer v2.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago
const App = () => {
  return (
    <AppearObserverProvider>
      <ScrollView>
        <TrackedComponent />
      </ScrollView>
    </AppearObserverProvider>
  )
}

const TrackedComponent = () => {
  const { refProps } = useAppearObserver({ 
    onAppear: useCallback(() => console.log('Hola!'), []) 
  })

  return <View { ...refProps } />
}
const App = () => {
  const scrollViewRef = useRef()
  const { interactionHandlers, interactionListeners } = useInteractionManager()

  return (
    <ScrollView ref={scrollViewRef} { ...interactionHandlers }>
      <TrackedComponent parentRef={scrollViewRef} interactionListeners={interactionListeners} />
    </ScrollView>
  )
}

const TrackedComponent = ({ parentRef, interactionListeners }: any) => {
  const { refProps } = useAppearObserver({
    parentRef,
    interactionListeners,
    onAppear: useCallback(() => console.log('Hi there!'), [])
  })

  return <View {...refProps} />
}
const App = () => {
  const scrollViewRef = useRef()

  return (
    <ScrollView ref={scrollViewRef}>
      <TrackedComponent parentRef={scrollViewRef} />
    </ScrollView>
  )
}

const TrackedComponent = ({ parentRef }: any) => {
  const { refProps } = useAppearObserver({
    parentRef,
    onAppear: useCallback(() => console.log('Hi there!'), []),
    options: {
      interactionModeEnabled: false,
    }
  })

  return <View {...refProps} />
}
const App = () => {
  return (
    <ScrollView>
      <TrackedComponent />
    </ScrollView>
  )
}

const TrackedComponent = () => {
  const { refProps } = useAppearObserver({
    onAppear: useCallback(() => console.log('Hi there!'), []),
    options: {
      interactionModeEnabled: false,
      useScreenIfNoParent: true,
    }
  })

  return <View {...refProps} />
}
const TrackedElement = ({ onAppear, onDisappear }: any) => {
  const isFocused = useIsFocused()

  const { refProps } = useAppearObserver({
    enabled: isFocused,
    onAppear,
    onDisappear,
    onDisable: onDisappear // Optional
  })

  return <View {...refProps} style={elementStyle} />
}
const App = () => {
  return (
    <AppearObserverProvider>
      {
        ({ 
           collapsable, 
           onLayout, 
           onScroll, 
           onScrollBeginDrag, 
           onScrollEndDrag, 
           onTouchStart, 
           onTouchEnd, 
           onTouchMove, 
           onTouchCancel, 
           onMomentumScrollEnd,
           interactionRecorders,
        }) => (
          <>
            <ScrollView 
              collapsable={collapsable} 
              onLayout={onLayout}
              onScrollBeginDrag={onScrollBeginDrag}
              onScrollEndDrag={onScrollEndDrag}
              onTouchStart={onTouchStart}
              onTouchEnd={onTouchEnd}
              onTouchMove={onTouchMove}
              onTouchCancel={onTouchCancel}
              onMomentumScrollEnd={onMomentumScrollEnd}
            >
              <TrackedComponent />
            </ScrollView>
            <ActionButton title="Scroll to top" onPress={() => {
              interactionRecorders.recordInteractionStart()
              scrollToTop()
            }} />
          </>
        )
      }
    </AppearObserverProvider>
  )
}
PropDescriptionDefault value
elementRefExternal ref to use instead of the one created by the hook. Use if you need to work with the tracked element by ref. No ref merging happens, the external one overrides the internal default.-
parentRefOptional props for usage without context. Provides parent element ref directly to the hook, overrides the ref supplied by context.-
onAppearCallback that triggers when the element comes into visibility.-
onDisappearCallback that triggers when the element disappears.-
onEnableCallback that fires when tracking gets enabled.-
onDisableCallback that fires when tracking gets disabled.-
enabledFlag which toggles tracking on and off. Useful when tracking needs to be reset, like in case with react navigation.true
interactionListenersSlot for interaction listeners passed from parent, for usage without context.-
OptionDescriptionDefault value
visibilityThresholdDefines what part of an element should be visible for it to trigger callback, from 0 to 1.0.001
intervalDelayDetermines a delay in milliseconds between visibility check repetitions.50
recalculateParentBoundariesTells whether observer should measure parent element boundaries on every on every check or measure once and cache.true
parentOffsetsSets additional static offsets added to the calculated parent boundaries. Useful if you need to consider the height of some header inside a ScrollView.0 for each corner
interactionModeEnabledMakes the tracking loop fire only when user interaction has happened. When disabled, the loop runs indefinitely until stopped by enabled prop.true
useScreenIfNoParentProvides an option to track the element relative to screen instead of parent element. Only works if parent ref is not passed.false
optimizeOutOfScreenDoubles the delay between loop cycles if the element is out of screen. A small optimization, candidate for improvement.true
PropDescription
refPropsRef and props that prevent element from being collapsed, see useObservableTargetRef
restartA handler to manually restart the observer. Can be used to trigger onAppear manually, for example when element's visibility hasn't changed, but its content has.
PropDescriptionDefault value
enableInteractionModeWhen true, the touch handlers are attached to the child element of the provider, and child observers run checks only upon touch interactions, stopping them after a period of inactivity.true
refAn external ref to pass to the provider's child element. Provider sets it's own ref by default, but can use external one for its operations. No ref merging happens, the external one overrides the internal default._
childrenReact element or render prop for manual setup._
offsetsAdditional static offsets added to the parent element's boundaries. Can be used to account for ScrollView headers in calculations.0 for each corner
onLayout, onScroll etcCallbacks to be passed to the child component on top of the provider's internal ones. When an element is wrapped with provider, you have to pass these callbacks to the provider instead of the target element, otherwise provider overwrites them.-
PropDescriptionDefault value
onScroll, onScrollBeginDrag, onScrollEndDrag etcExternal listeners to be attached alongside internal service ones.true
PropDescription
interactionHandlersCallbacks for interaction events that trigger to be attached to parent component. They trigger the tracking loop start.
interactionRecordersSeparate callbacks that can be attached to any event to mark it as interaction start or end. For example can serve to set 'Scroll to top' button press as interaction.
interactionListenersInteraction listeners to be passed to useAppearObserver hook of the tracked components so that they can listen to parent interactions.
PropDescriptionDefault value
refExternal ref to be used instead of the default one set up internally.-
PropDescription
refA regular React ref
collapsableSet to true, prevents native node from being collapsed.
onLayoutAlso serves for collapse prevention, as collapsable is currently only for Android.
2.1.2

1 year ago

2.1.1

1 year ago

2.0.2

1 year ago

2.1.0

1 year ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago