简体   繁体   English

*this* 对象在 React 组件中是“未定义的”

[英]*this* object is "undefined" inside React component

I am trying to build a utility function for my React project and it will be used within a React component to manage some behaviors.我正在尝试为我的 React 项目构建一个实用程序函数,它将在 React 组件中用于管理某些行为。

During the development process I discovered that the object this within my function (not a React component) returned undefined .在开发过程中,我发现我的函数(不是 React 组件)中的this对象返回了undefined I would use the keyword this because I would chain the functions (fn1().fn2().fn3()) to create a kind of declarative experience for those who want to use my function.我会使用关键字this ,因为我会链接函数 (fn1().fn2().fn3()) 来为那些想要使用我的函数的人创建一种声明式体验。

How to reproduce the behavior?如何重现行为?

  1. Create a new React project with the package "create-react-app"使用“create-react-app”包创建一个新的 React 项目

  2. Create a new JS file (choose the name you want)创建一个新的 JS 文件(选择你想要的名字)

  3. Inside the new JS file write a new function like this:在新的 JS 文件中编写一个新函数,如下所示:

     function foo() { console.log(this) }
  4. Call the function wherever you want: inside or outside a React component.随时随地调用该函数:在 React 组件内部或外部。 Eg.例如。 (index.js) (索引.js)

     import React, { Component } from "react"; import { foo } from "../../../validation/test"; import "./App.css"; class App extends Component { constructor(props) { super(props); this.state = {}; } render() { foo(); return <h1>Hello Stack Overflow Community....</h1>; } } export default App;
  5. Take a look to the browser console.查看浏览器控制台。

I tried in a node environment, but I'm not facing this problem.我在节点环境中尝试过,但我没有遇到这个问题。

What could be the cause?可能是什么原因?

Did you try foo.bind(this)() instead of foo()?你试过 foo.bind(this)() 而不是 foo() 吗? This way it gives the context to your function and this would refer to the class from which it was called from.通过这种方式,它为您的函数提供了上下文,这将引用从中调用它的类。

Now, after further research and reading, I think I am able to answer my own question.现在,经过进一步的研究和阅读,我想我可以回答我自己的问题了。

Why " this " was undefined in the case mentioned in my question?为什么在我的问题中提到的案例中未定义this ”?

The keyword this is always a reference to an object ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this ).关键字this始终是对对象的引用( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this )。 However, in my case I did not perform any operation necessary to assign the keyword this a reference to the function object, I only executed the function ( foo() ).但是,在我的情况下,我没有执行任何必要的操作来将关键字this分配给函数对象的引用,我只执行了函数( foo() )。

The image below shows the value of this keyword:下图显示了这个关键字的值:

在此处输入图片说明

Following that, instead:在此之后,改为:

  1. I created an instance of the foo() object (see the red arrow in the below image)我创建了一个foo()对象的实例(见下图中的红色箭头)
  2. I executed the foo2() function (instance of the foo() object) within a React component and as expected, the keyword this is now referenced to the function object and I will be able to make method chaining .我在 React 组件中执行了foo2()函数(foo() 对象的实例),正如预期的那样,关键字this现在被引用到函数对象,我将能够创建方法链接。

在此处输入图片说明

Any integration or correction of my answer will be appreciated.对我的答案的任何整合或更正将不胜感激。

Thanks to all contributors.感谢所有贡献者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM