Skip to content

Commit

Permalink
fix(weapp/qq): 修复新 props 系统在微信小程序下循环渲染时的问题。fix #4350
Browse files Browse the repository at this point in the history
1. 非 plain object 不去 diff,直接替换,避免小程序自身循环渲染对 new 对象的 bug
2. PropsManager.set 对 props 的计算太早,compid 可能被小程序的 diff 所改变,如果 compid 有变,需要在 observer 重新计算。
  • Loading branch information
Chen-jj committed Sep 5, 2019
1 parent 742ac61 commit ae66b35
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
11 changes: 10 additions & 1 deletion packages/taro-qq/src/create-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,17 @@ function bindProperties (weappComponentConf, ComponentClass, isPage) {
weappComponentConf.properties.compid = {
type: null,
value: null,
observer () {
observer (newVal, oldVal) {
initComponent.apply(this, [ComponentClass, isPage])
if (oldVal) {
const { extraProps } = this.data
const component = this.$component
propsManager.observers[newVal] = {
component,
ComponentClass: component.constructor
}
component.props = filterProps(component.constructor.defaultProps, propsManager.map[newVal], component.props, extraProps || null)
}
}
}
}
Expand Down
14 changes: 11 additions & 3 deletions packages/taro-qq/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,16 @@ function diffArrToPath (to, from, res = {}, keyPrev = '') {
res[targetKey] = toItem
} else {
// 对象
let shouldDiffObject = true
let shouldDiffObject = isPlainObject(toItem)

shouldDiffObject &&
Object.keys(fromItem).some(key => {
if (typeof toItem[key] === 'undefined' && typeof fromItem[key] !== 'undefined') {
shouldDiffObject = false
return true
}
})

if (shouldDiffObject) {
diffObjToPath(toItem, fromItem, res, `${targetKey}.`)
} else {
Expand All @@ -161,7 +164,9 @@ export function diffObjToPath (to, from, res = {}, keyPrev = '') {
const toItem = to[key]
const fromItem = from[key]
const targetKey = `${keyPrev}${key}`
if (toItem === fromItem) {
if (/^\$compid__/.test(key)) {
res[targetKey] = toItem
} else if (toItem === fromItem) {
continue
} else if (!hasProp.call(from, key)) {
res[targetKey] = toItem
Expand All @@ -187,13 +192,16 @@ export function diffObjToPath (to, from, res = {}, keyPrev = '') {
res[targetKey] = toItem
} else {
// 对象
let shouldDiffObject = true
let shouldDiffObject = isPlainObject(toItem)

shouldDiffObject &&
Object.keys(fromItem).some(key => {
if (typeof toItem[key] === 'undefined' && typeof fromItem[key] !== 'undefined') {
shouldDiffObject = false
return true
}
})

if (shouldDiffObject) {
diffObjToPath(toItem, fromItem, res, `${targetKey}.`)
} else {
Expand Down
11 changes: 10 additions & 1 deletion packages/taro-weapp/src/create-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,17 @@ function bindProperties (weappComponentConf, ComponentClass, isPage) {
weappComponentConf.properties.compid = {
type: null,
value: null,
observer () {
observer (newVal, oldVal) {
initComponent.apply(this, [ComponentClass, isPage])
if (oldVal) {
const { extraProps } = this.data
const component = this.$component
propsManager.observers[newVal] = {
component,
ComponentClass: component.constructor
}
component.props = filterProps(component.constructor.defaultProps, propsManager.map[newVal], component.props, extraProps || null)
}
}
}
weappComponentConf.properties.extraProps = {
Expand Down
14 changes: 11 additions & 3 deletions packages/taro-weapp/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,16 @@ function diffArrToPath (to, from, res = {}, keyPrev = '') {
res[targetKey] = toItem
} else {
// 对象
let shouldDiffObject = true
let shouldDiffObject = isPlainObject(toItem)

shouldDiffObject &&
Object.keys(fromItem).some(key => {
if (typeof toItem[key] === 'undefined' && typeof fromItem[key] !== 'undefined') {
shouldDiffObject = false
return true
}
})

if (shouldDiffObject) {
diffObjToPath(toItem, fromItem, res, `${targetKey}.`)
} else {
Expand All @@ -162,7 +165,9 @@ export function diffObjToPath (to, from, res = {}, keyPrev = '') {
const toItem = to[key]
const fromItem = from[key]
const targetKey = `${keyPrev}${key}`
if (toItem === fromItem) {
if (/^\$compid__/.test(key)) {
res[targetKey] = toItem
} else if (toItem === fromItem) {
continue
} else if (!hasProp.call(from, key)) {
res[targetKey] = toItem
Expand All @@ -189,13 +194,16 @@ export function diffObjToPath (to, from, res = {}, keyPrev = '') {
res[targetKey] = toItem
} else {
// 对象
let shouldDiffObject = true
let shouldDiffObject = isPlainObject(toItem)

shouldDiffObject &&
Object.keys(fromItem).some(key => {
if (typeof toItem[key] === 'undefined' && typeof fromItem[key] !== 'undefined') {
shouldDiffObject = false
return true
}
})

if (shouldDiffObject) {
diffObjToPath(toItem, fromItem, res, `${targetKey}.`)
} else {
Expand Down

0 comments on commit ae66b35

Please sign in to comment.