@bam.tech/flow-navigator

中文文档
0.0.2发布于 2024/03/15MITTypeScriptCommonJS零依赖近 30 天下载 6955

A flow navigator for react-navigation

此软件包已被标记为弃用

No longer maintained. It targets @react-navigation v6 and React Native 0.72. Build multi-step flows on @react-navigation/native-stack v7 instead.

原始使用文档
文档目录14 节
  1. Simplifying Flow Navigation in React Native
  2. Features
  3. Installation
  4. Usage
  5. Basic usage
  6. Define conditional steps
  7. Define steps with several screens
  8. Use cases
  9. API definition
  10. FlowNavigator
  11. useFlow
  12. Contributing
  13. Testing your changes
  14. Publishing the package

Alt text

Flow Navigator for React Navigation

Simplifying Flow Navigation in React Native

Flow Navigator provides a simplified API for managing navigation flows in your React Native applications with React Navigation. It abstracts the complexity of flow management, allowing individual screens to navigate through the flow using simple methods like goToNextStep and goToPreviousStep, without the need to understand the entire navigation stack, or knowing which page exactly is the next one.

Features

  • Simplified Flow Management: Get a comprehensive overview of your flow in a single location. This allows new developers to quickly understand the entire flow without the need to examine each page individually.
  • Separation of responsibility: Screens are not aware of their specific position in the flow. They don't need to know which page is next; they simply navigate to the next page in the flow.
  • Declarative Screen Ordering: Define the order of screens in your navigation flow declaratively, ensuring a clear and maintainable navigation structure.

Installation

The flow navigator is built on top of react-navigation's native stack navigator. So it requires both react-navigation and the native stack navigator installed.

To install the flow navigator, run:

yarn add @bam.tech/flow-navigator
# or
npm install @bam.tech/flow-navigator

Usage

Basic usage

import { createFlowNavigator } from '@bam.tech/flow-navigator';

const FlowNavigator = createFlowNavigator();

export const FlowNavigatorExample = () => {
   // Define your screens and their order in the flow
  return (
    <FlowNavigator.Navigator>
      <FlowNavigator.Screen name="Step1" component={Step1Page} />
      <FlowNavigator.Screen name="Step2" component={Step2Page} />
      <FlowNavigator.Screen name="Step3" component={Step2Page} />
    </FlowNavigator.Navigator>
  );
};

In each screen component, you can navigate through the flow using:

import { useFlow } from '@bam.tech/flow-navigator';

const Step1Page = () => {
  const { goToNextStep, goToPreviousStep, currentStep } = useFlow();

  return (
    <Button title="Go to next page" onPress={() => goToNextStep()} />
  )
};

You can find a fully working example in the example folder.

Alt text

Define conditional steps

In certain scenarios, a flow may include steps that are conditional. These steps might be dependent on user-specific conditions or based on whether certain actions have already been completed. You can manage such conditional steps declaratively in your navigation flow.

Here's an example where "Step 2" is conditionally displayed based on the hasToPassStep2 variable. This variable could be a piece of data fetched from the backend or a state within your application. In our case, we use jotai to store our user data locally.

import { createFlowNavigator } from '@bam.tech/flow-navigator';

export const flagAtom = atom(false);

const FlowNavigator = createFlowNavigator();

export const FlowNavigatorExample = () => {
  const [flag] = useAtom(flagAtom);

  return (
    <FlowNavigator.Navigator>
      <FlowNavigator.Screen name="Step1" component={Step1Page} />
      {flag && <FlowNavigator.Screen name="Step2" component={Step2Page} />}
      <FlowNavigator.Screen name="Step3" component={Step3Page} />
    </FlowNavigator.Navigator>
  );
};

In this example, the Step2 screen is only included in the flow if hasToPassStep2 evaluates to true.

You can enable or disable routes at anytime in your flow by setting your boolean state: setFlag(false)

import { useFlow } from '@bam.tech/flow-navigator';

export const Step1Page = () => {
  const {goBack, goToNextStep} = useFlow();
  const [flag] = useAtom(flagAtom);

  const onNextPress = async () => {
    setFlag(false);
    goToNextStep();
  };

  const onBackPress = () => {
    goBack();
  };

  return (
    <View style={styles.container}>
      <Text style={styles.pageTitle}>Current page: 1</Text>
      <FlowInfos />
      <Button title="next" onPress={onNextPress} />
      <Button title="back" onPress={onBackPress} />
    </View>
  );
};

You can check out a fully working example in the example folder

Define steps with several screens

In some scenarios, a single step in a flow may encompass several screens. To group these screens within one step, you have a couple of options: using Groups or Nested navigators. Examples of both approaches can be found in the example folder. We recommend using groups if they suit your use-case. However, one limitation to note is that the currentStep will reflect the name of the screen that is currently focused, not the group name. So all the screens in the step won't have the same currentStep value. With nested navigator, currentStep is the name of the subnavigator, which provides a more cohesive representation of the step. Note that all the pages of a subnavigator will correspond to one increment of the progress index, that will stay constant throughout the whole step.

Use cases

Flows are sequences of pages with a pre-defined order, guiding users through a specific process within your app. Whether complex or straightforward, flows are a fundamental part of the user experience in many applications. Here are some common examples where Flow Navigator can be particularly useful:

  • Onboarding flow
  • Post publication flow
  • Subscription flow
  • Shopping cart checkout process
  • Survey of feedback flow
  • Profile setup flow

API definition

FlowNavigator

The Flow Navigator is built upon the foundation of the native stack, it inherits the same API.

useFlow

Inside a screen defined below a Flow Navigator, you can use the useFlow useFlow provides the following helpers:

  • goToNextStep: To navigate to the next step in the flow, based on the order of the screens in the navigation flow.
  • goToPreviousStep: To navigate to the previous step in the flow, based on the order of the screens in the navigation flow.
  • quitFlow: To exit the flow.

useFlow also provides information about the current step of the flow. It contains the following properties:

  • currentStep: A string representing the identifier of the current step in the flow. Based on the name of the screen.
  • progress: A number indicating the progress through the flow. It is calculated as the ratio of the current index to the total number of routes.
  • canGoToPreviousStep: A boolean indicating whether navigation to a previous step is possible.
  • canGoToNextStep: A boolean indicating whether navigation to the next step is possible.

Contributing

Pull requests and feature suggestions are more than welcome!

Testing your changes

You can try out your changes in the example folder. Use yarn link to try out your local library version:

  1. At the root directory of @bam.tech/flow-navigator, run
  cd packages/lib
  yarn link
  1. Link in the example project:
  cd packages/example
  yarn link "@bam.tech/flow-navigator"
  1. Make your changes in the lib, and transpile the code from TS to JS, to be able to try out the changes in the example. You can run this command at the root of the repo
  yarn transpile:lib
  1. Unlink when done, once the changes are published:
  cd packages/example
  yarn unlink "@bam.tech/flow-navigator"
  yarn install

Publishing the package

  1. Increment the package.json in ./packages/lib/package.json
  2. Commit the change git commit -m "chore: bump version"
  3. Add a tag matching the version
   git tag vx.x.x && git push --tags
  1. Then publish the package: run this command at the root of the repo
  yarn publish:lib

版本与发布记录

@bam.tech/flow-navigator 共发布 9 个版本,首次发布于 2023/11/14;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.2latest2024/03/152 年前
0.0.12024/03/132 年前
1.0.72024/03/052 年前
1.0.62024/03/042 年前
1.0.42023/11/202 年前
1.0.32023/11/162 年前

关于 @bam.tech/flow-navigator 的常见问题

@bam.tech/flow-navigator 是什么?

A flow navigator for react-navigation注意:该包已被作者标记为弃用——No longer maintained. It targets @react-navigation v6 and React Native 0.72. Build multi-step flows on @react-navigation/native-stack v7 instead.

如何安装 @bam.tech/flow-navigator?

在项目目录运行 npm install @bam.tech/flow-navigator,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(*)、react-native(^0.72.6)、@react-navigation/native(^6.1.9)、@react-navigation/native-stack(^6.9.17)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bam.tech/flow-navigator 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bam.tech/flow-navigator 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bam.tech/flow-navigator 可以商用吗?

@bam.tech/flow-navigator 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bam.tech/flow-navigator 的下载量有多少?

根据 npm 官方下载统计,@bam.tech/flow-navigator 在 2026/10/08 – 2026/11/06 这 30 天内被下载 6,955 次,日均约 232 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bam.tech/flow-navigator 还在维护吗?

最新版本 0.0.2 发布于 2024/03/15(2 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/11/14。作者已将其标记为弃用,新项目建议寻找替代方案。