From adbc78d2e24469d485ee07337611b466d5cedfc0 Mon Sep 17 00:00:00 2001 From: chenjiajian <798095202@qq.com> Date: Mon, 19 Nov 2018 22:41:39 +0800 Subject: [PATCH] =?UTF-8?q?fix(taro-weapp):=20=E5=B0=8F=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=20diff=20=E5=9C=A8=E6=96=B0=E5=AF=B9=E8=B1=A1=E7=BC=BA?= =?UTF-8?q?=E5=A4=B1=E6=97=A7=E5=AF=B9=E8=B1=A1=E7=9A=84=E6=9F=90=E4=BA=9B?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E6=97=B6=EF=BC=8C=E4=B8=8D=E5=86=8D=E9=80=92?= =?UTF-8?q?=E5=BD=92=20diff=EF=BC=8C=E8=80=8C=E6=98=AF=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E8=B5=8B=E5=80=BC=E3=80=82fix=20#1058?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taro-weapp/src/lifecycle.js | 7 +++--- packages/taro-weapp/src/util.js | 34 ++++++++++++++++++++++------ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/packages/taro-weapp/src/lifecycle.js b/packages/taro-weapp/src/lifecycle.js index 5231db082cec..60a1dcb8779b 100644 --- a/packages/taro-weapp/src/lifecycle.js +++ b/packages/taro-weapp/src/lifecycle.js @@ -75,10 +75,11 @@ function doUpdate (component, prevProps, prevState) { return } if (typeof val === 'object') { + if (isEmptyObject(val)) return safeSet(_data, key, val) + val = shakeFnFromObject(val) - if (!isEmptyObject(val)) { - safeSet(_data, key, val) - } + // 避免筛选完 Fn 后产生了空对象还去渲染 + if (!isEmptyObject(val)) safeSet(_data, key, val) } else { safeSet(_data, key, val) } diff --git a/packages/taro-weapp/src/util.js b/packages/taro-weapp/src/util.js index 31015793534b..070c80d53755 100644 --- a/packages/taro-weapp/src/util.js +++ b/packages/taro-weapp/src/util.js @@ -121,7 +121,18 @@ function diffArrToPath (to, from, res = {}, keyPrev = '') { res[targetKey] = toItem } else { // 对象 - diffObjToPath(toItem, fromItem, res, `${targetKey}.`) + let shouldDiffObject = true + Object.keys(fromItem).some(key => { + if (typeof toItem[key] === 'undefined') { + shouldDiffObject = false + return true + } + }) + if (shouldDiffObject) { + diffObjToPath(toItem, fromItem, res, `${targetKey}.`) + } else { + res[targetKey] = toItem + } } } } @@ -142,11 +153,9 @@ export function diffObjToPath (to, from, res = {}, keyPrev = '') { const targetKey = `${keyPrev}${key}` if (toItem === fromItem) { continue - } else - if (!hasProp.call(from, key)) { + } else if (!hasProp.call(from, key)) { res[targetKey] = toItem - } else - if (typeof toItem !== typeof fromItem) { + } else if (typeof toItem !== typeof fromItem) { res[targetKey] = toItem } else { if (typeof toItem !== 'object') { @@ -167,8 +176,19 @@ export function diffObjToPath (to, from, res = {}, keyPrev = '') { if (!toItem || !fromItem) { res[targetKey] = toItem } else { - // 对象 - diffObjToPath(toItem, fromItem, res, `${targetKey}.`) + // 对象 + let shouldDiffObject = true + Object.keys(fromItem).some(key => { + if (typeof toItem[key] === 'undefined') { + shouldDiffObject = false + return true + } + }) + if (shouldDiffObject) { + diffObjToPath(toItem, fromItem, res, `${targetKey}.`) + } else { + res[targetKey] = toItem + } } } }