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

【67页-69页】计算属性 computed 与 lazy 思维有点跳 #45

Open
w457130328 opened this issue Feb 27, 2022 · 1 comment
Open

Comments

@w457130328
Copy link

w457130328 commented Feb 27, 2022

【67页】无缓存的 computed 实现,当 obj.fooobj.bar 值变更时都会执行副作用函数,这个执行没有任何意义,因为执行后的结果并不会被收集。

  • 处理方案:添加调度器,在调度器中不做任何处理。
const effectFn = effect(getter, {
  lazy: true,
  scheduler() {}
})

【69页】增加调度器的配置,这里调度器是 ”空参“,看书时比较困扰为什么要用 “空参”。

  • 建议对空参进行说明。或者就是先解决上面提到的问题自然就引出了空参调度器的配置。
@YagamiNewLight
Copy link

看到这一章的时候很好奇,为什么computed计算属性不用reactive实现呢:

const computedMemoVersion = (getter) => {
  const reactive = makeReactive({});
  effect(() => {
    reactive.value = getter();
  });
  return {
    get value() {
      return reactive.value;
    },
  };
};

经过我的测试,这样做既可以让计算属性在被effect嵌套的时候具有响应性,又能够在每次被读取值的时候保证getter函数不会被多次调用。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants