Coder Social home page Coder Social logo

react-native-intersection-observer's Introduction

react-native-intersection-observer

react-native-intersection-observer is a React Native implementation of Intersection Observer. An easier way to detect "View" exposure in complex application.

English | 中文

NPM

Installation

npm install rn-intersection-observer

Requirements

This module was developed and tested with dependencies below. May also works fine with older versions that support "measureInWindow".

React >= 16.13.1

React-native >= 0.63.4

Usage

Target view

import { IntersectionObserverView } from 'rn-intersection-observer';

// ...

<IntersectionObserverView
    scope="YourOwnScope"
    thresholds={[0.8]}
    onIntersectionChange={onTagIntersectionChange}
>
 {/* your own view */}
</IntersectionObserverView>

Trigger detection from React Native

import { IntersectionObserver } from 'rn-intersection-observer';

// ...

const onScroll = useCallback(
    (event) => {
        IntersectionObserver.emitEvent('YourOwnScope');
    },
    [],
);

return (
    <ScrollView onScroll={onScroll}>
        {/* Scroll view contains IntersectionObserverView */}
    </ScrollView>
);

Trigger detection from Native (Android example)

getReactApplicationContext()
  .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
  .emit("IntersectionObeserverEvent", { scope: 'YourOwnScope' });

Props & Params

1) IntersectionObserver / IntersectionObserverView

Props Params Type Description
scope string Scope of the target View, required in event trigger.
rootMargin {top: number, left: number, bottom: number, right: number} Distance from screen edge of detect area.
thresholds number[] Intersection ratios which should trigger intersection callbacks.
throttle number Throttle time between each detection(ms).

2) Intersection Callback (onIntersectionChange)

Callback parameter is an array containes info of each target which triggered intersection callback:

Params Params Type Description
boundingClientRect {top: number, left: number, bottom: number, right: number} Position of target View's edge.
intersectionRatio number Intersection ratio of target View in detect area
intersectionRect {top: number, left: number, bottom: number, right: number} Position of intersection area's edge.
target Ref Ref of target View
isInsecting boolean Determine current intersection ratio is larger than any threshold.

PS:Different from IntersectionObserver, IntersectionObserverView provides single parameter.

License

MIT

react-native-intersection-observer's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

react-native-intersection-observer's Issues

Multiple observers is not detecting

Hi,

In a scrollview I want to listen to multiple set of intersections. Its working for two intersections but more than two is not working.

`
const onScroll = useCallback(
(event) => {
IntersectionObserver.emitEvent('YourOwnScope1');
IntersectionObserver.emitEvent('YourOwnScope2');
IntersectionObserver.emitEvent('YourOwnScope3');
IntersectionObserver.emitEvent('YourOwnScope4');
},
[],
);

`

Please help me with this.

Thank You.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.