Skip to content

Commit

Permalink
fix: shallowXXX not track
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Oct 19, 2021
1 parent 87d243f commit dd285aa
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/reactivity/src/baseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ function createGetter(isReadonly = false, shallow = false) {

const res = Reflect.get(target, key, receiver);

// 问题:为什么是 readonly 的时候不做依赖收集呢
// readonly 的话,是不可以被 set 的, 那不可以被 set 就意味着不会触发 trigger
// 所有就没有收集依赖的必要了

if (!isReadonly) {
// 在触发 get 的时候进行依赖收集
track(target, "get", key);
}

if (shallow) {
return res;
}
Expand All @@ -50,14 +59,6 @@ function createGetter(isReadonly = false, shallow = false) {
return isReadonly ? readonly(res) : reactive(res);
}

// 问题:为什么是 readonly 的时候不做依赖收集呢
// readonly 的话,是不可以被 set 的, 那不可以被 set 就意味着不会触发 trigger
// 所有就没有收集依赖的必要了
if (!isReadonly) {
// 在触发 get 的时候进行依赖收集
track(target, "get", key);
}

return res;
};
}
Expand Down

0 comments on commit dd285aa

Please sign in to comment.