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

使用useReactive 处理对象时会出现this指向错误 #1946

Closed
projectcss opened this issue Oct 31, 2022 · 2 comments · Fixed by #1952
Closed

使用useReactive 处理对象时会出现this指向错误 #1946

projectcss opened this issue Oct 31, 2022 · 2 comments · Fixed by #1952
Assignees

Comments

@projectcss
Copy link

案例

import { useRef, useState } from "react";
import { useReactive } from "./useReactive";
const parent = {
  name: "parent",
  get value() {
    return this.name;
  }
};
const child = {
  name: "child"
};
export default function App() {
  const proxy = useReactive(parent);
  Object.setPrototypeOf(child, proxy);
  console.log((child as any).value); // parent
  return <div className="App"></div>;
}

当我访问child.value时我希望的是访问他自身的name属性,而不是原型上的name属性,我查看了一下useReactive源码,有一处地方有个疑问,为什么Reflect.get(target, key)不添加他的第三那个参数,即receiver,去修改this的指向?
image

案例地址

codesandbox

@hchlq
Copy link
Collaborator

hchlq commented Nov 2, 2022

如果走到后面,直接返回 res 就好了哈

@crazylxr
Copy link
Collaborator

crazylxr commented Nov 3, 2022

代码已经 fix 合并了,下个版本发布就好了

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

Successfully merging a pull request may close this issue.

3 participants