Files
react-native/packages/rn-tester/js/examples/SectionList/SectionList-onViewableItemsChanged.js
T
Mike Vitousek 8c2a4d0d26 Annotate React hooks in xplat, defaulting to any
Summary:
Add explicit annotations to React hook callbacks as required for Flow's Local Type Inference project. This codemod prepares the codebase to match Flow's new typechecking algorithm. The new algorithm will make Flow more reliable and predictable.

This diff adds `any` or `$FlowFixMe` in cases where more precise types could not be determined.

Details:
- Codemod script: `.facebook/flowd codemod annotate-react-hooks ../../xplat/js --default-any --write`
- Local Type Inference announcement: [post](https://fb.workplace.com/groups/flowlang/posts/788206301785035)
- Support group: [Flow Support](https://fb.workplace.com/groups/flow)

drop-conflicts
bypass-lint

Reviewed By: SamChou19815

Differential Revision: D41231214

fbshipit-source-id: d5f5ce8d61020baa1138292c9e9f1c69dffd324c
2022-11-11 17:04:38 -08:00

86 lines
2.2 KiB
JavaScript

/**
* 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
*/
import type {ViewToken} from 'react-native/Libraries/Lists/ViewabilityHelper';
import SectionListBaseExample from './SectionListBaseExample';
import * as React from 'react';
import {SectionList, StyleSheet, View} from 'react-native';
const VIEWABILITY_CONFIG = {
minimumViewTime: 1000,
viewAreaCoveragePercentThreshold: 100,
waitForInteraction: true,
};
type SectionListProps = React.ElementProps<typeof SectionList>;
type ViewabilityConfig = $PropertyType<SectionListProps, 'viewabilityConfig'>;
export function SectionList_onViewableItemsChanged(props: {
viewabilityConfig: ViewabilityConfig,
offScreen?: ?boolean,
horizontal?: ?boolean,
useScrollRefScroll?: ?boolean,
}): React.Node {
const {viewabilityConfig, offScreen, horizontal, useScrollRefScroll} = props;
const [output, setOutput] = React.useState('');
const exampleProps = {
onViewableItemsChanged: (info: {
changed: Array<ViewToken>,
viewableItems: Array<ViewToken>,
...
}) =>
setOutput(
info.viewableItems
.filter(viewToken => viewToken.index != null && viewToken.isViewable)
.map(viewToken => viewToken.item)
.join(', '),
),
viewabilityConfig,
horizontal,
};
const ref = React.useRef<any>(null);
const onTest =
useScrollRefScroll === true
? () => {
ref?.current?.getScrollResponder()?.scrollToEnd();
}
: null;
return (
<SectionListBaseExample
ref={ref}
exampleProps={exampleProps}
onTest={onTest}
testOutput={output}>
{offScreen === true ? <View style={styles.offScreen} /> : null}
</SectionListBaseExample>
);
}
const styles = StyleSheet.create({
offScreen: {
height: 1000,
},
});
export default {
title: 'SectionList On Viewable Items Changed',
name: 'SectionList_onViewableItemsChanged',
render: function (): React.Element<
typeof SectionList_onViewableItemsChanged,
> {
return (
<SectionList_onViewableItemsChanged
viewabilityConfig={VIEWABILITY_CONFIG}
/>
);
},
};