Skip to content

Commit

Permalink
fix(rn): reLaunch error (#10802)
Browse files Browse the repository at this point in the history
* fix(rn): relunch error

* fix(router-rn): judgment method of isTabPage

* feat(rn): add entryPagePath support

* fix: push bug

* fix: 缩写
  • Loading branch information
zhiqingchen authored Dec 13, 2021
1 parent 6f6d04e commit c72188c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
25 changes: 16 additions & 9 deletions packages/taro-router-rn/src/rootNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function navigate (option: NavigateOption | NavigateBackOption, method: N
navigationRef.current?.dispatch(StackActions.push(routeParam.pageName, routeParam.params))
} else if (method === 'redirectTo') {
navigationRef.current?.dispatch(StackActions.replace(routeParam.pageName, routeParam.params))
} else if (method === 'switchTab') {
} else if (method === 'switchTab' || (method === 'reLaunch' && isTabPage(path))) {
const states = navigationRef.current?.getRootState()
if (states?.routes[0].name !== 'tabNav') {
states && states?.routes.length > 1 && navigationRef.current?.dispatch(StackActions.popToTop())
Expand All @@ -72,15 +72,22 @@ export function navigate (option: NavigateOption | NavigateBackOption, method: N
} else if (method === 'navigateBack') {
const number = (option as NavigateBackOption).delta ? (option as NavigateBackOption).delta : 1
const states = navigationRef.current?.getRootState()
const index = number && ((states && states.index < number) ? states?.index : number)
navigationRef.current?.dispatch(StackActions.pop(index))
if (states?.index === 0) {
errMsg = 'navigateBack:fail cannot navigate back at first page.'
} else {
const index = number && ((states && states.index < number) ? states?.index : number)
navigationRef.current?.dispatch(StackActions.pop(index))
}
} else if (method === 'reLaunch') {
if (isTabPage()) {
isTabPage(path)
? navigationRef.current?.navigate(routeParam.pageName, routeParam.params)
: navigationRef.current?.dispatch(StackActions.push(routeParam.pageName, routeParam.params))
// tabbar to stack page
navigationRef.current?.dispatch(StackActions.replace(routeParam.pageName, routeParam.params))
} else {
navigationRef.current?.dispatch(StackActions.popToTop())
// stack to stack page
const states = navigationRef.current?.getRootState()
if (states?.index !== 0) {
navigationRef.current?.dispatch(StackActions.popToTop())
}
navigationRef.current?.dispatch(StackActions.replace(routeParam.pageName, routeParam.params))
}
}
Expand Down Expand Up @@ -112,7 +119,7 @@ export function isTabPage (path = ''): boolean {
const tabPages = getTabBarPages()
let pageName = ''
if (path) {
pageName = camelCase(path.startsWith('/') ? path : `/${path}`)
pageName = camelCase((path.startsWith('/') ? path : `/${path}`).split('?')[0])
} else {
const route: Record<string, any> = navigationRef.current?.getCurrentRoute() || {}
pageName = route?.name || ''
Expand All @@ -129,7 +136,7 @@ export function getCurrentRoute () {
if (item.name === 'tabNav') {
const index = item.state?.index ?? 0
const tabRoutes: Record<string, any>[] = item.state?.routes ?? []
tabRoutes && routeKeys.push(tabRoutes[index].key)
tabRoutes?.[index] && routeKeys.push(tabRoutes[index].key)
} else {
routeKeys.push(item.key)
}
Expand Down
8 changes: 7 additions & 1 deletion packages/taro-router-rn/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export interface RouterConfig {
rnConfig?: RNConfig,
initParams?:Record<string, any>, // 原生启动传递的参数
initPath?: string, // 原生启动时传入的参数路径
entryPagePath?: string, // 默认启动路径
}

export function createRouter (config: RouterConfig): React.ReactNode {
Expand Down Expand Up @@ -171,14 +172,19 @@ function getTabItem (config: RouterConfig, tabName: string) {
return tabItem
}

let initRoute

function getInitRouteName (config: RouterConfig) {
let initRoute = ''
if (initRoute) return initRoute
const initPath = config.initPath || ''
const rn = config.rnConfig || {}
if (initPath) {
initRoute = handleUrl(initPath).pageName
} else if (rn?.initialRouteName) {
initRoute = camelCase(rn.initialRouteName)
} else if (config.entryPagePath) {
const entryPagePath = config.entryPagePath.startsWith('/') ? config.entryPagePath : `/${config.entryPagePath}`
initRoute = config.pages.find(p => p.pagePath === entryPagePath)?.name
} else {
initRoute = config.pages[0].name
}
Expand Down
1 change: 1 addition & 0 deletions packages/taro-runtime-rn/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function createReactNativeApp (component: React.ComponentClass, config: R
const routerConfig: RouterConfig = {
tabBar: config.appConfig.tabBar,
pages: config.pageList,
entryPagePath: config.appConfig.entryPagePath,
window: config.appConfig.window,
linkPrefix: config.appConfig.linkPrefix || [],
rnConfig: config.appConfig.rn || {}
Expand Down
3 changes: 2 additions & 1 deletion packages/taro-runtime-rn/src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ export interface AppConfig {
designWidth: number,
deviceRatio: Record<number, number>,
linkPrefix: string[],
rn?: any
rn?: any,
entryPagePath?: string,
}

export interface RNAppConfig {
Expand Down

0 comments on commit c72188c

Please sign in to comment.