react-native-in-port
TypeScript icon, indicating that this package has built-in type declarations

0.6.4 • Public • Published

react-native-in-port

Use LightHouse and Ship to detect if a children component is inside the (View)port.

242895017-24eb55c6-abbe-410e-a363-c179f17a9c9d

Installation

npm install react-native-in-port

Example

const ScrollViewExample = () => {
  return (
    // LightHouse's child component can only be ScrollView or FlatList.
    // When scroll and focus events occur, LightHouse asks the Ship if it is within a (view)port.
    // throttleTime: dafault === 500
    <LightHouse throttleTime={100}>
      <ScrollView style={styles.scrollView} scrollEventThrottle={1}>
        {colors.map((color, index) => (
          <Ship
            key={index}
            detectPercent={50}
            // Adjust the detection condition by adjusting the viewport area.
            viewportMargin={{ top: headerHeight, bottom: 70 }}
            onPort={(state) => {
              // You may call the tracking API in this part.
              // --------------------------------------------
              // await analytics().logEvent('viewed', {
              //   useId: 3745092,
              //   description: 'User viewed an ad space.',
              // })
              // --------------------------------------------

              const { isInPort, inPortCount } = state;

              // Return values can be passed as child props.
              return {
                nextProps: { backgroundColor: isInPort ? 'gray' : color },
                isValidInPort: inPortCount < 1,
              };
            }}
          >
            <Box backgroundColor={color} />
          </Ship>
        ))}
      </ScrollView>
    </LightHouse>
  );
};

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library

Package Sidebar

Install

npm i react-native-in-port

Weekly Downloads

1

Version

0.6.4

License

MIT

Unpacked Size

56.5 kB

Total Files

38

Last publish

Collaborators

  • wayne-kim