From 064f15b2d74bd1bb74c49026a5d76b9858a14917 Mon Sep 17 00:00:00 2001 From: xiaolei <1017653702@qq.com> Date: Fri, 7 Mar 2025 13:08:14 +0800 Subject: [PATCH 1/4] [fix]rn-ref-wx:for --- .../platform/builtInMixins/refsMixin.ios.js | 49 +++++++++++-------- .../lib/template-compiler/compiler.js | 2 +- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/packages/core/src/platform/builtInMixins/refsMixin.ios.js b/packages/core/src/platform/builtInMixins/refsMixin.ios.js index 9a761624d..2fdcb5be1 100644 --- a/packages/core/src/platform/builtInMixins/refsMixin.ios.js +++ b/packages/core/src/platform/builtInMixins/refsMixin.ios.js @@ -28,29 +28,38 @@ export default function getRefsMixin () { }) }, __getRefVal (type, selectorsConf, refFnId) { - if (!this.__refCache[refFnId]) { - this.__refCache[refFnId] = (instance) => { - selectorsConf.forEach((item = []) => { - const [prefix, selectors = ''] = item - if (selectors) { - selectors.trim().split(/\s+/).forEach(selector => { - const refKey = prefix + selector - const refVal = { type, instance, refFnId } - this.__refs[refKey] = this.__refs[refKey] || [] - if (instance) { // mount - this.__refs[refKey].push(refVal) - } else { // unmount - const index = this.__refs[refKey].findIndex(item => item.refFnId === refFnId) - if (index > -1) { - this.__refs[refKey].splice(index, 1) - } + const refFn = (instance) => { + selectorsConf.forEach((item = []) => { + const [prefix, selectors = ''] = item + if (selectors) { + selectors.trim().split(/\s+/).forEach(selector => { + const refKey = prefix + selector + const refVal = { + type, + instance, + uid: refFnId || refFn + } + this.__refs[refKey] = this.__refs[refKey] || [] + if (instance) { // mount + this.__refs[refKey].push(refVal) + } else { // unmount + const index = this.__refs[refKey].findIndex(item => item.uid === refFnId || item.uid === refFn) + if (index > -1) { + this.__refs[refKey].splice(index, 1) } - }) - } - }) + } + }) + } + }) + } + if (refFnId) { + if (!this.__refCache[refFnId]) { + this.__refCache[refFnId] = refFn } + return this.__refCache[refFnId] + } else { + return refFn } - return this.__refCache[refFnId] }, __selectRef (selector, refType, all = false) { const splitedSelector = selector.match(/(#|\.)?[^.#]+/g) || [] diff --git a/packages/webpack-plugin/lib/template-compiler/compiler.js b/packages/webpack-plugin/lib/template-compiler/compiler.js index c6723292d..031eab468 100644 --- a/packages/webpack-plugin/lib/template-compiler/compiler.js +++ b/packages/webpack-plugin/lib/template-compiler/compiler.js @@ -1834,7 +1834,7 @@ function processRefReact (el, meta) { const selectorsConf = selectors.map(item => `["${item.prefix}", ${item.selector}]`) addAttrs(el, [{ name: 'ref', - value: `{{ this.__getRefVal('${type}', [${selectorsConf}], 'ref_fn_${++refId}') }}` + value: `{{ this.__getRefVal('${type}', [${selectorsConf}], ${all ? '' : `ref_fn_${++refId}` }) }}` }]) } From 6efeb348f78977452336129928b72b90570556cb Mon Sep 17 00:00:00 2001 From: xiaolei <1017653702@qq.com> Date: Fri, 7 Mar 2025 13:17:19 +0800 Subject: [PATCH 2/4] [lint] --- packages/webpack-plugin/lib/template-compiler/compiler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/webpack-plugin/lib/template-compiler/compiler.js b/packages/webpack-plugin/lib/template-compiler/compiler.js index 031eab468..01ec031db 100644 --- a/packages/webpack-plugin/lib/template-compiler/compiler.js +++ b/packages/webpack-plugin/lib/template-compiler/compiler.js @@ -1834,7 +1834,7 @@ function processRefReact (el, meta) { const selectorsConf = selectors.map(item => `["${item.prefix}", ${item.selector}]`) addAttrs(el, [{ name: 'ref', - value: `{{ this.__getRefVal('${type}', [${selectorsConf}], ${all ? '' : `ref_fn_${++refId}` }) }}` + value: `{{ this.__getRefVal('${type}', [${selectorsConf}], ${all ? '' : `ref_fn_${++refId}`}) }}` }]) } From bc9e8f57cd3e55c411493d7197f1a3ba16ea230e Mon Sep 17 00:00:00 2001 From: xiaolei <1017653702@qq.com> Date: Fri, 7 Mar 2025 13:35:31 +0800 Subject: [PATCH 3/4] [chore]comment --- packages/webpack-plugin/lib/template-compiler/compiler.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/webpack-plugin/lib/template-compiler/compiler.js b/packages/webpack-plugin/lib/template-compiler/compiler.js index 01ec031db..15b51a02c 100644 --- a/packages/webpack-plugin/lib/template-compiler/compiler.js +++ b/packages/webpack-plugin/lib/template-compiler/compiler.js @@ -1811,7 +1811,9 @@ function processRefReact (el, meta) { const selectors = [] /** - * selectorsConf: [type, [[prefix, selector], [prefix, selector]]] + * refFnId 在 for 循环当中为空,避免 refFn 在运行时被不同 ref 缓存共用,其他场景有唯一值,用作缓存 key + * + * selectorsConf: [type, [[prefix, selector], [prefix, selector]], refFnId?] */ if (!val) { const rawId = el.attrsMap.id From a7c2f0b57fd154d71ffbac7877c2143f8673cff6 Mon Sep 17 00:00:00 2001 From: xiaolei <1017653702@qq.com> Date: Fri, 7 Mar 2025 15:45:31 +0800 Subject: [PATCH 4/4] lint --- packages/webpack-plugin/lib/template-compiler/compiler.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/webpack-plugin/lib/template-compiler/compiler.js b/packages/webpack-plugin/lib/template-compiler/compiler.js index 15b51a02c..474d0e69e 100644 --- a/packages/webpack-plugin/lib/template-compiler/compiler.js +++ b/packages/webpack-plugin/lib/template-compiler/compiler.js @@ -1812,7 +1812,6 @@ function processRefReact (el, meta) { /** * refFnId 在 for 循环当中为空,避免 refFn 在运行时被不同 ref 缓存共用,其他场景有唯一值,用作缓存 key - * * selectorsConf: [type, [[prefix, selector], [prefix, selector]], refFnId?] */ if (!val) {