as

Settings
Sign out
Notifications
Alexa
亚马逊应用商店
AWS
文档
Support
Contact Us
My Cases
新手入门
设计和开发
应用发布
参考
支持

shopify-react-native-performance-navigation-drawer

shopify-react-native-performance-navigation-drawer

shopify-react-native-performance-navigation-drawer是基于@shopify/react-native-performance-navigation的扩展库,具有适用于@react-navigation/drawer库的其他帮助程序方法。

安装

  1. package.json文件中添加JavaScript库依赖项。

    已复制到剪贴板。

    "dependencies": {
    ...
    "@shopify/react-native-performance": "npm:@amazon-devices/shopify__react-native-performance@~2.0.0",
    "@shopify/react-native-performance-navigation": "npm:@amazon-devices/shopify__react-native-performance-navigation@~3.0.0",
    "@shopify/react-native-performance-navigation-drawer": "npm:@amazon-devices/shopify__react-native-performance-navigation-drawer@~3.0.0",
    "@amazon-devices/react-native-reanimated": "~2.0.0",
    "@amazon-devices/react-native-gesture-handler": "~2.0.0"
    },
    
  2. 运行以下命令,将npm设置为使用版本7之前的行为来处理专用于当前项目的对等依赖项。这样可在处理可能不兼容或过时的依赖项时,以更宽松的条件进行程序包安装。

    已复制到剪贴板。

    npm config set legacy-peer-deps=true --location=project
    
  3. 将Babel模块解析器插件添加到package.json文件中的devDependencies部分内:

    已复制到剪贴板。

    "devDependencies": {
      ...
      "babel-plugin-module-resolver": "~5.0.2",
    },
    
  4. babel.config.js文件的plugin部分为@amazon-devices/shopify__react-native-performance*配置别名。

    已复制到剪贴板。

    plugins: [
    ["module-resolver", {
        "alias": {
        "~@amazon-devices/shopify__react-native-performance(.*)": "@shopify/react-native-performance/\\1"
        }
    }],
    "@amazon-devices/react-native-reanimated/plugin"
    ]
    
  5. 对于使用@amazon-devices/react-navigation_<X>@2.0.0库(其中<X>表示stacknative组件等特定组件)的项目,请将这些库同时包含在package.json文件的dependenciesoverrides部分中。强烈建议升级到@amazon-devices/react-navigation_<X>@7.0.0,它引入了@amazon-devices/react-native-reanimated支持,以增强性能和动画。

    已复制到剪贴板。

     "dependencies": {
       ...
       "@amazon-devices/react-navigation__<X>": "~2.0.0",
       ...
     },
     ...
     "overrides": {
       ...
       "@amazon-devices/react-navigation__<X>": "~2.0.0",
       ...
     },
    
  6. 使用npm install命令重新安装依赖项。

  7. 使用重置缓存选项npm start -- --reset-cache重启Metro。

示例

使用通过createProfiledDrawerNavigator函数创建的抽屉导航器,分析渲染托管在不同选项卡中的屏幕所需的时间。

已复制到剪贴板。


import React, { useCallback, useContext, useState } from 'react';
import { createProfiledDrawerNavigator } from '@shopify/react-native-performance-navigation-drawer';
import {
  LogLevel,
  PerformanceProfiler,
  RenderPassReport,
} from '@shopify/react-native-performance';
import { NavigationContainer } from '@amazon-devices/react-navigation__native';
import { StyleSheet, Text, View } from 'react-native';
import { ReactNavigationPerformanceView } from '@shopify/react-native-performance-navigation';
import { GestureHandlerRootView } from '@amazon-devices/react-native-gesture-handler';

type ReportContextState = {
  saveReport: (report: RenderPassReport) => void;
  findReport: (destinationScreen: string, sourceScreen?: string) => void;
};

const INITIAL_STATE = {
  saveReport: () => {},
  findReport: () => {},
};

const ReportContext = React.createContext<ReportContextState>(INITIAL_STATE);

const useReportContext = () => {
  return useContext(ReportContext);
};

const DrawerScreenA = () => {
  const { findReport } = useReportContext();
  const report = findReport('drawerScreenA', 'Drawer');

  return (
    <ReactNavigationPerformanceView
      screenName="drawerScreenA"
      interactive={true}
    >
      <View style={styles.container}>
        <Text style={styles.label}>正在测量渲染时间</Text>
        <Text style={styles.label}>
          每次"抽屉"导航"抽屉屏幕A"后, 都会
          生成新报告"
        </Text>
        <Text style={styles.text}>{JSON.stringify(report)}</Text>
      </View>
    </ReactNavigationPerformanceView>
  );
};

const DrawerScreenB = () => {
  const { findReport } = useReportContext();
  const report = findReport('drawerScreenB', 'Drawer');

  return (
    <ReactNavigationPerformanceView
      screenName="drawerScreenB"
      interactive={true}
    >
      <View style={[styles.container]}>
        <Text style={styles.label}>正在测量渲染时间</Text>
        <Text style={styles.label}>
          每次"抽屉"导航"抽屉屏幕B"后, 都会
          生成新报告"
        </Text>
        <Text style={styles.text}>{JSON.stringify(report)}</Text>
      </View>
    </ReactNavigationPerformanceView>
  );
};

const Drawer = createProfiledDrawerNavigator();

export const App = () => {
  const onReportPrepared = (report: RenderPassReport) => {
    saveReport(report);
  };
  const [reports, setReports] = useState<RenderPassReport[]>([]);

  const saveReport = useCallback((report: RenderPassReport) => {
    setReports((prev) => {
      const filtered = prev.filter(
        (r) =>
          r.sourceScreen !== report.sourceScreen ||
          r.destinationScreen !== report.destinationScreen,
      );
      return [...filtered, report];
    });
  }, []);

  const findReport = useCallback(
    (destinationScreen: string, sourceScreen?: string) => {
      return reports.find(
        (r) =>
          r.sourceScreen === sourceScreen &&
          r.destinationScreen === destinationScreen,
      );
    },
    [reports],
  );
  return (
    <GestureHandlerRootView style={{flex: 1}}>
      <ReportContext.Provider value={{ saveReport, findReport }}>
        <PerformanceProfiler
          onReportPrepared={onReportPrepared}
          logLevel={LogLevel.Debug}
        >
          <NavigationContainer>
            <Drawer.Navigator>
              <Drawer.Screen name="抽屉屏幕A" component={DrawerScreenA} />
              <Drawer.Screen name="抽屉屏幕B" component={DrawerScreenB} />
            </Drawer.Navigator>
          </NavigationContainer>
        </PerformanceProfiler>
      </ReportContext.Provider>
    </GestureHandlerRootView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 20,
    backgroundColor: '#283593',
  },
  label: {
    color: 'white',
    fontSize: 28,
  },
  text: {
    fontSize: 20,
    color: 'white',
    marginTop: 20,
  },
});

API参考

查看专门文档,了解有关此库、API参考等内容的信息。 官方react-native-performance-navigation-drawer文档(仅提供英文版)。

函数

函数 描述 平台支持
createDrawerNavigator 该实用工具打包了原有的createDrawerNavigator,可让您分析渲染托管在不同选项卡中的屏幕需要的时间。 所有平台

实现详情

  • 由于react-navigation的抽屉导航中的焦点管理存在已知问题,目前无法在Vega上生成用于在抽屉导航中导航的渲染通道报告

支持的版本

程序包版本 基于 @amazon-devices/react-native-kepler版本
3.0.0 @shopify/react-native-performance-navigation-drawer v3.0.0 2.0.x

其他资源

有关其他库的信息,请参阅支持的第三方库和服务


Last updated: 2025年9月30日