我在哪里可以找到有关为什么将对象方法分配为React中的DOM事件处理程序时的“此上下文”为什么不确定的文档?

发布于 2025-02-08 16:59:58 字数 1306 浏览 4 评论 0原文

React组件似乎会改变在DOM事件处理程序中分配的的行为,但是我找不到任何详细介绍此行为的文档。

例如,当将对象的方法用作带有香草JS的DOM事件处理程序时,上下文仍然是对象:

function SomeClass() {}
SomeClass.prototype.showThis = function() { console.log(this) };
let o = new SomeClass();
<button onclick="o.showThis()">Show "this"</button>

但是,反应改变了此行为,使得上下文变得不确定:

function SomeClass() {}
SomeClass.prototype.showThis = function() { console.log(this) };
let o = new SomeClass();

function App() {
  return (
       <button onClick={o.showThis}>Show this</button>
  );
}

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root"></div>

解释这一点的文档在哪里? (我可以推测为什么会发生这种情况,但想知道React文档中的何处。)

It seems that React components change the behavior of how this is assigned in DOM event handlers, but I cannot find any documentation that details this behavior.

For example, when using an object's method as a DOM event handler with vanilla JS, the this context remains as the object:

function SomeClass() {}
SomeClass.prototype.showThis = function() { console.log(this) };
let o = new SomeClass();
<button onclick="o.showThis()">Show "this"</button>

React, however, changes this behavior such that the this context becomes undefined:

function SomeClass() {}
SomeClass.prototype.showThis = function() { console.log(this) };
let o = new SomeClass();

function App() {
  return (
       <button onClick={o.showThis}>Show this</button>
  );
}

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root"></div>

Where is the documentation that explains this? (I can surmise why this may occur, but would like to know where this is covered in the React docs.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若沐 2025-02-15 16:59:58

这不是什么特别的反应。这是由于以下事实:借助内联处理程序,当功能被调用时,它被作为对象的一部分调用:

onclick="o.showThis()"
         ^ object
                   ^^ invocation

但是在React代码中,它并未被作为对象的一部分调用 - 而是在对象的一部分。 o.showthis 被以a 回调的方式传递,然后像其他任何回调一样称呼 - 例如callback()或类似的东西(而不是作为一个对象)。如果您非常稍微调整代码,以便将Shackthis作为对象的一部分调用,则它将显示与Inline Handler相同的行为:

onClick={() => o.showThis()}
               ^ object
                         ^^ invocation

function SomeClass() {}
SomeClass.prototype.showThis = function() { console.log(this) };
let o = new SomeClass();

function App() {
  return (
       <button onClick={() => o.showThis()}>Show this</button>
  );
}

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root"></div>

因此,没有什么可以专门解释它的 - 这是基础JavaScript的行为。

如果将函数称为对象的一部分,并且您处于严格的模式,则类似地,您会在内联处理程序中获得未定义的

'use strict';

function SomeClass() {}
SomeClass.prototype.showThis = function() {
  console.log(this)
};
const o = new SomeClass();

const theFn = o.showThis;
<button onclick="theFn()">Show "this"</button>

This is not anything special to React. This is due to the fact that, with the inline handler, the moment the function gets invoked, it's being invoked as part of an object:

onclick="o.showThis()"
         ^ object
                   ^^ invocation

But in the React code, it's not being invoked as part of an object - rather, o.showThis is passed as a callback which is then called like any other callback - like callback() or something of the sort (and not as part of an object). If you very slightly tweak the code so that the showThis is invoked as part of an object, it'll show the same behavior as the inline handler:

onClick={() => o.showThis()}
               ^ object
                         ^^ invocation

function SomeClass() {}
SomeClass.prototype.showThis = function() { console.log(this) };
let o = new SomeClass();

function App() {
  return (
       <button onClick={() => o.showThis()}>Show this</button>
  );
}

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root"></div>

So there's nothing for React specifically to explain about it - it's a behavior of the underlying JavaScript.

You would similarly get undefined in the inline handler if the function was called not as part of an object, and you were in strict mode.

'use strict';

function SomeClass() {}
SomeClass.prototype.showThis = function() {
  console.log(this)
};
const o = new SomeClass();

const theFn = o.showThis;
<button onclick="theFn()">Show "this"</button>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文