diff --git a/packages/core/src/core/proxy.js b/packages/core/src/core/proxy.js index 4b6f0ae18..998fe8089 100644 --- a/packages/core/src/core/proxy.js +++ b/packages/core/src/core/proxy.js @@ -171,9 +171,7 @@ export default class MpxProxy { // web中BEFORECREATE钩子通过vue的beforeCreate钩子单独驱动 this.callHook(BEFORECREATE) setCurrentInstance(this) - if (isReact) { - this.parentProvides = this.options.parentProvides - } else { + if (!isReact) { this.parent = this.resolveParent() this.provides = this.parent ? this.parent.provides : Object.create(null) } diff --git a/packages/core/src/platform/patch/getDefaultOptions.ios.js b/packages/core/src/platform/patch/getDefaultOptions.ios.js index 25832380c..b46dc0460 100644 --- a/packages/core/src/platform/patch/getDefaultOptions.ios.js +++ b/packages/core/src/platform/patch/getDefaultOptions.ios.js @@ -195,7 +195,7 @@ const instanceProto = { } } -function createInstance ({ propsRef, type, rawOptions, currentInject, validProps, components, pageId, intersectionCtx, relation }) { +function createInstance ({ propsRef, type, rawOptions, currentInject, validProps, components, pageId, intersectionCtx, relation, parentProvides }) { const instance = Object.create(instanceProto, { dataset: { get () { @@ -282,6 +282,8 @@ function createInstance ({ propsRef, type, rawOptions, currentInject, validProps } const proxy = instance.__mpxProxy = new MpxProxy(rawOptions, instance) + // 在 created 之前设置 parentProvides + proxy.parentProvides = parentProvides proxy.created() Object.assign(proxy, { @@ -446,8 +448,7 @@ export function getDefaultOptions ({ type, rawOptions = {}, currentInject }) { let isFirst = false if (!instanceRef.current) { isFirst = true - rawOptions.parentProvides = parentProvides - instanceRef.current = createInstance({ propsRef, type, rawOptions, currentInject, validProps, components, pageId, intersectionCtx, relation }) + instanceRef.current = createInstance({ propsRef, type, rawOptions, currentInject, validProps, components, pageId, intersectionCtx, relation, parentProvides }) } const instance = instanceRef.current useImperativeHandle(ref, () => { @@ -541,7 +542,7 @@ export function getDefaultOptions ({ type, rawOptions = {}, currentInject }) { root = cloneElement(root, rootProps) } - const provides = useMemo(() => proxy.provides, [proxy.provides]) + const provides = proxy.provides if (provides) { root = createElement(ProviderContext.Provider, { value: provides }, root) }