项目作者: ralali

项目描述 :
rematch plugin for react-navigation 5
高级语言: TypeScript
项目地址: git://github.com/ralali/rematch-navigation-plugin.git
创建时间: 2020-08-10T04:48:43Z
项目社区:https://github.com/ralali/rematch-navigation-plugin

开源协议:

下载


@ralali/rematch-navigation-plugin

MIT license

React Navigation 5 plugin for Rematch

Installation

  1. yarn add @ralali/rematch-navigation-plugin
  2. or
  3. npm i @ralali/rematch-navigation-plugin

Peer Dependencies

  • react >= 16.7.0-alpha
  • react native ( recommend >= 0.55.0 )
  • @react-native-async-storage/async-storage>=1.11.0
  • @react-navigation/native >=5.0.0
  • @rematch/core >= 1.0.0

Usage

register

register navigation ref in your root NavigationContainer

  1. import { register } from '@ralali/rematch-navigation-plugin';
  2. import { NavigationContainer } from '@react-navigation/native';
  3. return (
  4. <NavigationContainer ref={register} ></NavigationContainer>
  5. );

navigationPlugin

  1. import { init } from '@rematch/core';
  2. import { navigationPlugin } from '@ralali/rematch-navigation-plugin';
  3. const store = init({
  4. ...,
  5. plugins: [navigationPlugin]
  6. });
  7. export default store;

in your model

  1. ...,
  2. effects: (dispatch) => ({
  3. exampleNavigate: async () => {
  4. dispatch.nav.navigate('SomewhereScreen');
  5. }
  6. });

navigationRef

if you want to access NavigationContainer ref, simply use

  1. import { navigationRef } from '@ralali/rematch-navigation-plugin';
  2. import { CommonActions } from '@react-navigation/native';
  3. function navigateWithoutComponent() {
  4. if (navigationRef) {
  5. navigationRef.dispatch(
  6. CommonActions.navigate({
  7. name: 'HomeScreen',
  8. params
  9. })
  10. );
  11. }
  12. }

useExitHandler

options defaultValue ReturnType
duration 1500 number
message Press once again to exit string
handler () => BackHandler.exitApp() void

we already handle for android backPress on root stack if there is no screen left.
Usually if there is screen left in stack navigator, when pressing backPress two times, application will exit

simply put this method in your Root Navigation component

  1. import { useExitHandler } from '@ralali/rematch-navigation-plugin'
  2. // without custom message
  3. useExitHandler();
  4. // if you want to give custom message
  5. useExitHandler({
  6. message: 'Becareful, press once again will terminate this app'
  7. });
  8. return (
  9. <NavigationContainer
  10. ...
  11. );

by default, we use BackHandler.exitApp() from react-native to handle application exit, hovewer use this function won’t totally kill your application

if you want to use another handler for making application totally killed, we suggest to use react-native-exit-app

  1. import RNExitApp from 'react-native-exit-app';
  2. useExitHandler({
  3. message: 'Becareful, press once again will terminate this app',
  4. handler: () => RNExitApp.exitApp()
  5. });

usePersistNav

This feature is only active in development mode

  1. import { usePersistNav, register } from '@ralali/rematch-navigation-plugin';
  2. const { initialState, setNavState, isNavReady } = usePersistNav();
  3. /**
  4. * partial docs from https://reactnavigation.org/docs/state-persistence/
  5. *
  6. * Because the state is restored asynchronously
  7. * the app must render an empty/loading view for a moment before we have the initial state
  8. * to handle this, you can return a loading view when isNavReady is false:
  9. *
  10. */
  11. if (!isNavReady) {
  12. return null;
  13. or
  14. return <ActivityIndicator ></ActivityIndicator>;
  15. }
  16. return (
  17. <NavigationContainer
  18. ref={register}
  19. initialState={initialState}
  20. onStateChange={(state) => {
  21. setNavState(state);
  22. }}
  23. >
  24. ...
  25. );

useScreenAnalytics

since screen name on tab can duplicate, different with screne name on stack, this is a useful function to take IdenticalRoute

IdenticalRoute Description ReturnType
resultName if the first slice routes type is tab, we will append the type into resultName

returning latest route name is not enough for us to identify screen activity

example:

with tab: [MainTab] - All
without tab: CovidInfoScreen
string
closestRoute take the last 2 routes from NavigationState.routes ShortRoute[]
routes return only name and type from NavigationState.routes ShortRoute[]

example usage

  1. import { useScreenAnalytics, navigationRef } from '@ralali/rematch-navigation-plugin';
  2. import { setCurrentScreen } from 'your/helpers';
  3. const { subscribeState, routeNameRef } = useScreenAnalytics((route) => {
  4. const { resultName, closestRoute } = route;
  5. /**
  6. * use your analytics function here
  7. /*
  8. setCurrentScreen(resultName);
  9. });
  10. return (
  11. <NavigationContainer
  12. onReady={() => {
  13. routeNameRef.current = navigationRef.getCurrentRoute().name;
  14. }}
  15. onStateChange={(state) => {
  16. subscribeState();
  17. setNavState(state);
  18. }}
  19. >
  20. ...
  21. );