/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
*/
import {RNTesterThemeContext} from '../../components/RNTesterTheme';
import * as React from 'react';
import {
type ElementRef,
useContext,
useLayoutEffect,
useRef,
useState,
} from 'react';
import {Button, ScrollView, StyleSheet, Text, View} from 'react-native';
export const name = 'IntersectionObserver Benchmark';
export const title = name;
export const description =
'Example of using IntersectionObserver to observe a large amount of UI elements';
export function render(): React.Node {
return ;
}
const ROWS = 100;
const COLUMNS = 5;
function IntersectionObserverBenchark(): React.Node {
const [isObserving, setObserving] = useState(false);
return (
<>
{Array(ROWS)
.fill(null)
.map((_, row) => (
{Array(COLUMNS)
.fill(null)
.map((_2, column) => (
))}
))}
>
);
}
function Item({index, observe}: {index: number, observe: boolean}): React.Node {
const theme = useContext(RNTesterThemeContext);
const ref = useRef>();
useLayoutEffect(() => {
const element = ref.current;
if (!observe || !element) {
return;
}
const observer = new IntersectionObserver(
entries => {
// You can inspect the actual entries here.
// We don't log them by default to avoid the logs themselves to degrade
// performance.
},
{
threshold: [0, 1],
},
);
// $FlowExpectedError
observer.observe(element);
return () => {
observer.disconnect();
};
}, [observe]);
return (
{index + 1}
);
}
const styles = StyleSheet.create({
buttonContainer: {
padding: 10,
},
row: {
flexDirection: 'row',
},
item: {
flex: 1,
padding: 12,
margin: 5,
},
itemText: {
fontSize: 22,
textAlign: 'center',
},
});