-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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): 修复、迭代生命周期 #6996
fix(runtime): 修复、迭代生命周期 #6996
Changes from all commits
1694463
74d0f67
ec3a3a9
fa8f768
e5e0eee
56be5d6
775a663
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { isFunction, isArray } from '@tarojs/shared' | ||
import { PageContext, R as React } from './react' | ||
import { getPageInstance, injectPageInstance } from './common' | ||
import { PageLifeCycle } from './instance' | ||
|
@@ -19,19 +20,34 @@ const taroHooks = (lifecycle: keyof PageLifeCycle) => { | |
inst = Object.create(null) | ||
} | ||
|
||
inst = inst! | ||
|
||
// callback is immutable but inner function is up to date | ||
const callback = (...args: any) => fnRef.current(...args) | ||
if (lifecycle !== 'onShareAppMessage') { | ||
(inst![lifecycle] as any) = [ | ||
...((inst![lifecycle] as any) || []), | ||
callback | ||
] | ||
if (lifecycle === 'onShareAppMessage') { | ||
inst[lifecycle] = callback | ||
} else { | ||
inst![lifecycle] = callback | ||
if (isFunction(inst[lifecycle])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Chen-jj 如果上面 common.ts 里修复了, 页面实例就没有默认的 onShareAppMessage 回调了, 在函数式组件里 inst[lifecycle] 永远是 undefined, 我们还是应该用 lifecycle 这个字符串上来判断, 以实现页面第一次初始化时设置 onShareAppMessage/onShareTimeline/onAddToFavorites There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. createPageConfig 时 inst 是 undefined,小程序 onLoad 时才会绑定。 |
||
(inst[lifecycle] as any) = [inst[lifecycle], callback] | ||
} else { | ||
(inst[lifecycle] as any) = [ | ||
...((inst[lifecycle] as any) || []), | ||
callback | ||
] | ||
} | ||
} | ||
if (first) { | ||
injectPageInstance(inst!, id) | ||
} | ||
return () => { | ||
const inst = getPageInstance(id) | ||
const list = inst![lifecycle] | ||
if (list === callback) { | ||
(inst![lifecycle] as any) = undefined | ||
} else if (isArray(list)) { | ||
(inst![lifecycle] as any) = list.filter(item => item !== callback) | ||
} | ||
} | ||
}, []) | ||
} | ||
} | ||
|
@@ -58,6 +74,10 @@ export const useOptionMenuClick = taroHooks('onOptionMenuClick') | |
|
||
export const usePullIntercept = taroHooks('onPullIntercept') | ||
|
||
export const useShareTimeline = taroHooks('onShareTimeline') | ||
|
||
export const useAddToFavorites = taroHooks('onAddToFavorites') | ||
|
||
export const useReady = taroHooks('onReady') | ||
|
||
export const useRouter = (dynamic = false) => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Chen-jj 这里不应该是
component.prototype.onShareAppMessage
吗?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
component.onShareAppMessage
是给 Vue 用的,根据 prototype 判断可以加下,那么 Class Component 就不用配置 enable 了。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
记录一下:这样还是不完美,HOC 如 connect 后的页面组件会跪,还是要编译时处理一下以自动兼容大部分情况。