Skip to content

Commit

Permalink
fix(runtime): 修复 app 不能使用Show/Hide钩子的问题,fix #8348 (#8430)
Browse files Browse the repository at this point in the history
* fix(runtime): 修复 app 不能使用Show/Hide钩子的问题,fix #8348

* fix: ci
  • Loading branch information
Chen-jj authored Jan 4, 2021
1 parent 252dd08 commit 8a4bf12
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
4 changes: 3 additions & 1 deletion packages/taro-runtime/src/dsl/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { getPageInstance, injectPageInstance } from './common'
import { PageLifeCycle } from './instance'
import { Current } from '../current'

export const HOOKS_APP_ID = 'taro-app'

const taroHooks = (lifecycle: keyof PageLifeCycle) => {
return (fn: Function) => {
const id = React.useContext(PageContext)
const id = React.useContext(PageContext) || HOOKS_APP_ID

// hold fn ref and keep up to date
const fnRef = React.useRef(fn)
Expand Down
22 changes: 20 additions & 2 deletions packages/taro-runtime/src/dsl/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { isFunction, ensure, EMPTY_OBJ } from '@tarojs/shared'
import { Current } from '../current'
import { AppInstance, ReactPageComponent, PageProps, Instance, ReactAppInstance } from './instance'
import { document } from '../bom/document'
import { injectPageInstance } from './common'
import { getPageInstance, injectPageInstance } from './common'
import { isBrowser } from '../env'
import { options } from '../options'
import { Reconciler } from '../reconciler'
import { Reconciler, CurrentReconciler } from '../reconciler'
import { incrementId } from '../utils'
import { HOOKS_APP_ID } from './hooks'

function isClassComponent (R: typeof React, component): boolean {
return isFunction(component.render) ||
Expand Down Expand Up @@ -247,6 +248,9 @@ export function createReactApp (App: React.ComponentClass, react: typeof React,
if (app != null && isFunction(app.componentDidShow)) {
app.componentDidShow(options)
}

// app useDidShow
triggerAppHook('componentDidShow')
}
},

Expand All @@ -258,10 +262,24 @@ export function createReactApp (App: React.ComponentClass, react: typeof React,
if (app != null && isFunction(app.componentDidHide)) {
app.componentDidHide(options)
}

// app useDidHide
triggerAppHook('componentDidHide')
}
}
})

function triggerAppHook (lifecycle) {
const instance = getPageInstance(HOOKS_APP_ID)
if (instance) {
const app = ref.current
const func = CurrentReconciler.getLifecyle(instance, lifecycle)
if (Array.isArray(func)) {
func.forEach(cb => cb.apply(app))
}
}
}

Current.app = app
return Current.app
}

0 comments on commit 8a4bf12

Please sign in to comment.