Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-rn): 修复 getCurrentPages 页面实例错误bug #9319

Merged
merged 2 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/taro-router-rn/src/rootNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,19 @@ export function isTabPage (path = ''): boolean {
export function getCurrentRoute () {
const routeState = navigationRef.current?.getRootState()
const routes = routeState?.routes
const routeName: string[] = []
const routeKeys: string[] = []
if (routes) {
routes.forEach(item => {
if (item.name === 'tabNav') {
const index = item.state?.index ?? 0
const names = item.state?.routeNames ?? []
names && routeName.push(names[index])
const tabRoutes: Record<string, any>[] = item.state?.routes ?? []
tabRoutes && routeKeys.push(tabRoutes[index].key)
} else {
routeName.push(item.name)
routeKeys.push(item.key)
}
})
}
return routeName
return routeKeys
}

export const getRouteEventChannel = (routeChannel) => {
Expand Down
1 change: 1 addition & 0 deletions packages/taro-runtime-rn/src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface Instance<T = {}> extends Component<T>, PageLifeCycle {
export interface PageInstance extends PageLifeCycle {
config: PageConfig
route: string
options: Record<string, any>
getOpenerEventChannel?(): Record<string, any>
}

Expand Down
12 changes: 6 additions & 6 deletions packages/taro-runtime-rn/src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,12 @@ export function createPageConfig (Page: any, pageConfig: PageConfig): any {

setPageInstance () {
const pageRef = this.screenRef
const { params = {}, key = '' } = this.props.route
// 和小程序的page实例保持一致
const inst: PageInstance = {
config: pageConfig,
route: pagePath,
options: params,
onReady () {
const page = pageRef.current
if (page != null && isFunction(page.componentDidMount)) {
Expand Down Expand Up @@ -226,9 +228,8 @@ export function createPageConfig (Page: any, pageConfig: PageConfig): any {
}
}
// 存储对应小程序的实例
setPageObject(inst, pageId)
setPageObject(inst, key)

const { params = {} } = this.props.route
Current.router = {
params: params,
path: pagePath
Expand Down Expand Up @@ -511,16 +512,15 @@ export function setBackgroundTextStyle (options: TextStyleOption) {

export function getCurrentPages () {
const pages: PageInstance[] = []
const routeNames = getCurrentRoute()
if (routeNames && routeNames.length > 0) {
routeNames.forEach(item => {
const routes = getCurrentRoute()
if (routes && routes.length > 0) {
routes.forEach(item => {
const inst = getPageObject(item)
inst && pages.push(inst)
})
} else { // 第一次初始化时,getCurrentRoute会为空
const inst = Current.page
inst && pages.push(inst)
}

return pages
}