shopify-react-native-performance-lists-profiler
shopify-react-native-performance-lists-profiler
开放Beta测试文档 作为预发布开放Beta测试的一项内容,亚马逊提供了此技术文档。随着亚马逊收到反馈并对功能进行迭代,所描述的这些功能可能会发生变化。有关最新功能的信息,请参阅发布说明。
@amazon-devices/shopify__react-native-performance-lists-profiler是位于@shopify/react-native-performance-navigation上的扩展库,提供用于分析FlatList和FlashList组件的实用工具。
安装
- 在
package.json文件中添加JavaScript库依赖项。"dependencies": { ... "@amazon-devices/react-navigation__native": "~2.0.0", "@amazon-devices/react-navigation__stack": "~2.0.0", "@amazon-devices/shopify__flash-list": "~2.0.0", "@shopify/react-native-performance-lists-profiler": "npm:@amazon-devices/shopify__react-native-performance-lists-profiler@~1.1.0", "@amazon-devices/keplerscript-turbomodule-api": "~1.0.0" }, - 使用
npm install命令重新安装依赖项。
示例
import React, {
createContext,
ReactNode,
useCallback,
useContext,
useState,
} from 'react';
import {
Button,
FlatList,
ListRenderItem,
StyleSheet,
Text,
TouchableOpacity,
View,
} from 'react-native';
import { NavigationContainer } from '@amazon-devices/react-navigation__native';
import {
createStackNavigator,
StackScreenProps,
} from '@amazon-devices/react-navigation__stack';
import {
FlashListPerformanceView,
FlatListPerformanceView,
ListsProfiler,
} from '@shopify/react-native-performance-lists-profiler';
import {
FlashList,
ListRenderItem as FlashListRenderItem,
} from '@amazon-devices/shopify__flash-list';
interface BlankArea {
start: number;
end: number;
}
interface ListsProfilerContextType {
listName: string;
tti: number | undefined;
blankArea: BlankArea | undefined;
resetContext: () => void;
onInteractive: (tti: number, listName: string) => void;
onBlankArea: (start: number, end: number, listName: string) => void;
}
const ListsProfilerContext = createContext<ListsProfilerContextType | null>(
null,
);
interface ListsProfilerContextProviderProps {
children: ReactNode;
}
const ListsProfilerContextProvider = ({
children,
}: ListsProfilerContextProviderProps) => {
const [listName, setListName] = useState('');
const [tti, setTti] = useState<number>();
const [blankArea, setBlankArea] = useState<BlankArea>();
const resetContext = useCallback(() => {
setListName('');
setTti(undefined);
setBlankArea(undefined);
}, []);
const onInteractive = useCallback((tti: number, listName: string) => {
setTti(tti);
setListName(listName);
}, []);
const onBlankArea = useCallback(
(start: number, end: number, listName: string) => {
setBlankArea({ start, end });
setListName(listName);
},
[],
);
return (
<ListsProfilerContext.Provider
value={{
listName,
tti,
blankArea,
resetContext,
onInteractive,
onBlankArea,
}}
>
{children}
</ListsProfilerContext.Provider>
);
};
type StackParamList = {
FlashListScreen: undefined;
FlatListScreen: undefined;
HomeScreen: undefined;
};
const Stack = createStackNavigator<StackParamList>();
const NavigationTree = () => {
const { onInteractive, onBlankArea } = useContext(
ListsProfilerContext,
) as ListsProfilerContextType;
return (
<ListsProfiler onInteractive={onInteractive} onBlankArea={onBlankArea}>
<NavigationContainer>
<Stack.Navigator initialRouteName="HomeScreen">
<Stack.Screen
name="HomeScreen"
component={HomeScreen}
options={{ headerShown: false }}
/>
<Stack.Screen name="FlatListScreen" component={FlatListScreen} />
<Stack.Screen name="FlashListScreen" component={FlashListScreen} />
</Stack.Navigator>
</NavigationContainer>
</ListsProfiler>
);
};
const HomeScreen = ({
navigation,
}: StackScreenProps<StackParamList, 'HomeScreen'>) => {
const { resetContext } = useContext(
ListsProfilerContext,
) as ListsProfilerContextType;
const onNavigate =
(screenName: 'FlatListScreen' | 'FlashListScreen') => () => {
resetContext();
navigation.navigate(screenName);
};
return (
<View style={styles.container}>
<Text style={styles.text}>FlatList Example</Text>
<Button title="FlatListScreen" onPress={onNavigate('FlatListScreen')} />
<Text style={styles.text}>FlashList Example</Text>
<Button title="FlashListScreen" onPress={onNavigate('FlashListScreen')} />
</View>
);
};
export interface Item {
bgColor: string;
title: string;
height: number;
}
const listData = Array.from({ length: 1000 }, (_, i) => ({
title: `Item ${i + 1}`,
bgColor: `#${Math.floor(Math.random() * 16777215).toString(16)}`,
height: Math.floor(Math.random() * (140 - 60) + 60),
}));
interface ListItemProps {
item: Item;
}
const ListItem = ({ item }: ListItemProps) => {
return (
<TouchableOpacity
style={{ backgroundColor: item.bgColor, height: item.height }}
>
<Text style={styles.itemText}>{item.title}</Text>
</TouchableOpacity>
);
};
const DataDisplay = () => {
const { listName, tti, blankArea } = useContext(
ListsProfilerContext,
) as ListsProfilerContextType;
return (
<View style={styles.display}>
<Text style={styles.text}>List name: {listName}</Text>
<View style={styles.spacer} />
<Text style={styles.text}>TTI: {tti ?? 'n/a'}</Text>
<View style={styles.spacer} />
<Text style={styles.text}>
Blank area:{' '}
{blankArea ? JSON.stringify(blankArea, undefined, 0) : 'n/a'}
</Text>
</View>
);
};
const FlatListScreen = () => {
const renderItem: ListRenderItem<Item> = useCallback(
({ item }) => <ListItem item={item} />,
[],
);
return (
<View style={styles.listContainer}>
<DataDisplay />
<FlatListPerformanceView listName="flatlist">
<FlatList data={listData} renderItem={renderItem} />
</FlatListPerformanceView>
</View>
);
};
const FlashListScreen = () => {
const renderItem: FlashListRenderItem<Item> = useCallback(
({ item }) => <ListItem item={item} />,
[],
);
return (
<View style={styles.listContainer}>
<DataDisplay />
<FlashListPerformanceView listName="flashlist">
<FlashList data={listData} renderItem={renderItem} />
</FlashListPerformanceView>
</View>
);
};
export const App = () => {
return (
<ListsProfilerContextProvider>
<NavigationTree />
</ListsProfilerContextProvider>
);
};
const styles = StyleSheet.create({
container: {
backgroundColor: '#fff',
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
display: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
paddingVertical: 20,
},
itemText: {
fontSize: 24,
color: '#fff',
fontWeight: '500',
},
listContainer: {
flex: 1,
},
spacer: {
width: 32,
},
text: {
fontSize: 30,
fontWeight: '500',
},
});
API参考
查看专门文档,了解有关此库、API参考等内容的信息。 官方react-native-perfonce-lists-profiler文档(仅提供英文版)。
组件
| 组件 | 描述 |
|---|---|
ListsProfiler |
用于收集性能指标的组件。请挂载到应用树的高层位置 |
FlatListPerformanceView |
FlatList组件的包装器,可启动性能指标收集。 |
FlashListPerformanceView |
FlashList组件的包装器,可启动性能指标收集。 |
ListsProfiler属性
| 属性 | 描述 |
|---|---|
onInteractive |
当所分析列表变为交互式时触发回调。用于以自定义方式处理分析结果。该属性有两个参数: TTI代表到达可交互状态的时间,listName是性能视图中定义的列表的名称。 |
onBlankArea |
当列表滚动时,每一帧都会触发回调函数,即便不存在空白区域也会触发。用于以自定义方式处理分析结果。该属性有三个参数: offsetStart - 是屏幕顶部可见的空白区域(在向上移动时)。如果值大于0,则可以看见。 offsetEnd - 这是屏幕底部可见的空白区域(在向下移动时)。如果值大于0,则可以看见。 listName - 性能视图中定义的列表的名称 |
FlatListPerformanceView
| 属性 | 描述 |
|---|---|
listName |
在回调onInteractive和onBlankArea中使用的列表的名称。 |
onInteractive |
当所分析列表变为交互式时触发回调。如果您想避免使用ListsProfiler,可以直接在FlatListPerformanceView上使用。该属性有两个参数: TTI - 代表项目达到可以交互的状态所需的时间 listName - 在FlatListPerformanceView中定义的列表的名称 |
onBlankArea |
当列表滚动时,每一帧都会触发回调函数,即便不存在空白区域也会触发。如果您想避免使用ListsProfiler,可以直接在FlatListPerformanceView上使用。属性有三个参数:offsetStart是屏幕顶部可见的空白区域(在向上移动时)。如果值大于0,则可以看见。 offsetEnd - 这是屏幕底部可见的空白区域(在向下移动时)。如果值大于0,则可以看见。 listName - 在FlatListPerformanceView中定义的列表的名称 |
FlashListPerformanceView
| 属性 | 描述 | 平台支持 |
|---|---|---|
listName |
在回调onInteractive和onBlankArea中使用的列表的名称。 |
所有平台 |
实现详情
- 该库上游版本的文档错误地指出
FlashListPerformanceViewAPI与FlatListPerformanceView相同:onInteractive和onBlankArea只能直接与FlatListPerformanceView一起使用。
支持的版本
| 程序包名称 | 亚马逊NPM库版本 | Vega OS内部版本号 | Vega SDK版本 | 发布说明 |
|---|---|---|---|---|
@amazon-devices/shopify__react-native-performance-lists-profiler |
2.0.1+4.1.2 | OS 1.1 (201010438050) |
0.20 |
其他资源
有关其他库的信息,请参阅支持的第三方库和服务。
Last updated: 2025年9月30日

