Skip to content

Commit

Permalink
fix(weapp): setData 前需要深拷贝衍生自 state 的 data, fix #5012
Browse files Browse the repository at this point in the history
对 state 的操作直接影响 data,因为 data 和 state 引用一样,因此需要深拷贝再 setData。

去除 shakeFnFromObject 的锅。
  • Loading branch information
Chen-jj authored and ZakaryCode committed Dec 15, 2019
1 parent c6da0da commit 8c25b0d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
5 changes: 3 additions & 2 deletions packages/taro-weapp/src/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '@tarojs/taro'
import PropTypes from 'prop-types'
import { componentTrigger } from './create-component'
import { shakeFnFromObject, isEmptyObject, diffObjToPath, isFunction, isUndefined, isArray } from './util'
import { cloneDeep, isEmptyObject, diffObjToPath, isFunction, isUndefined, isArray } from './util'
import { enqueueRender } from './render-queue'

const isDEV = typeof process === 'undefined' ||
Expand Down Expand Up @@ -164,7 +164,8 @@ function doUpdate (component, prevProps, prevState) {
if (typeof val === 'object') {
if (isEmptyObject(val)) return safeSet(_data, key, {})

// 避免筛选完 Fn 后产生了空对象还去渲染
val = cloneDeep(val)

if (!isEmptyObject(val)) safeSet(_data, key, val)
} else {
safeSet(_data, key, val)
Expand Down
9 changes: 3 additions & 6 deletions packages/taro-weapp/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,18 @@ export function isArray (arg) {
return Array.isArray(arg)
}

export function shakeFnFromObject (obj) {
export function cloneDeep (obj) {
let newObj
if (isArray(obj)) {
newObj = []
const len = obj.length
for (let i = 0; i < len; i++) {
newObj.push(shakeFnFromObject(obj[i]))
newObj.push(cloneDeep(obj[i]))
}
} else if (isPlainObject(obj)) {
newObj = {}
for (const key in obj) {
if (isFunction(obj[key])) {
continue
}
const ret = shakeFnFromObject(obj[key])
const ret = cloneDeep(obj[key])
newObj[key] = ret
}
} else {
Expand Down

0 comments on commit 8c25b0d

Please sign in to comment.