Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

多处 setData 容易踩坑! #13

Open
longyiyiyu opened this issue Jul 29, 2019 · 1 comment
Open

多处 setData 容易踩坑! #13

longyiyiyu opened this issue Jul 29, 2019 · 1 comment
Labels
experience learn from this

Comments

@longyiyiyu
Copy link
Contributor

多处 setData 的场景:

  1. 按顺序处理数据,处理完一个数据就 setData 一下,这个其实是错误的编码,还会有性能问题! -- 这种场景应该用 const obj = {},所有数据都处理完再一次性 setData 来解决
  2. 有异步代码时,比如 setTimeout,这个时候如果用 setTimeout 函数外的闭包变量来 setData 的话,就会很危险,因为可能会用旧的数据覆盖setTimeout 期间其他地方 setData 的数据!
@longyiyiyu longyiyiyu added the experience learn from this label Jul 29, 2019
@longyiyiyu
Copy link
Contributor Author

longyiyiyu commented Jul 29, 2019

对于复杂数据,多处 setData 没有问题!所以切记,不要轻易替换复杂数据的指针!比如:

const { o } = this.data;
this.setData({
  o: o.concat([1, 2, 3]),
});

虽然麻烦,真的不如下面这个代码:

const { o } = this.data;
[1, 2, 3].forEach(it => {
  o.push(it);
});
this.setData({
  o,
});

这样子可以避免很多的隐藏坑!

@longyiyiyu longyiyiyu changed the title 多处 setData 容易踩坑! 多处 setData 容易踩坑! Jul 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
experience learn from this
Projects
None yet
Development

No branches or pull requests

1 participant