我在哪里可以找到有关为什么将对象方法分配为React中的DOM事件处理程序时的“此上下文”为什么不确定的文档?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是什么特别的反应。这是由于以下事实:借助内联处理程序,当功能被调用时,它被作为对象的一部分调用:
但是在React代码中,它并未被作为对象的一部分调用 - 而是在对象的一部分。 o.showthis 被以a 回调的方式传递,然后像其他任何回调一样称呼 - 例如
callback()
或类似的东西(而不是作为一个对象)。如果您非常稍微调整代码,以便将Shackthis
作为对象的一部分调用,则它将显示与Inline Handler相同的行为:因此,没有什么可以专门解释它的 - 这是基础JavaScript的行为。
如果将函数称为对象的一部分,并且您处于严格的模式,则类似地,您会在内联处理程序中获得
未定义的
。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:
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 - likecallback()
or something of the sort (and not as part of an object). If you very slightly tweak the code so that theshowThis
is invoked as part of an object, it'll show the same behavior as the inline handler: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.