简体   繁体   English

在React项目中,“ this”转换为“ undefined”

[英]In React project, “this” converts into “undefined”

A very simple code 一个非常简单的代码

var App = React.createClass({
    handleForm: (e) => {
        e.preventDefault();
    },
    render: () => {
        return (
            <form>
                <button type="submit" onClick={this.handleForm}>Go</button>
            </form>
        );
    }
});

Gets converted into 转换成

// ...
_react2['default'].createElement(
    'button',
    { type: 'submit', onClick: undefined.handleFormSubmit },
    'Go'
)

But why? 但为什么? And do I need to bind all the things explicitly (including this.setState which I can't get working for the same reason)? 我是否需要显式绑定所有事物(包括this.setState相同原因而无法使用的this.setState )?

I'm using react 0.13.3 with webpack 1.12.2 and babel-loader 5.3.2. 我正在使用带有Webpack 1.12.2和babel-loader 5.3.2的react 0.13.3。 Haven't faced such a problem before. 以前没有遇到过这样的问题。

When you use an arrow function as the value of a property in an object literal, the binding to this is that of the function's declaration scope just like any arrow function. 当您使用箭头函数作为对象文字中属性的值时, this的绑定就是该函数的声明范围的绑定,就像任何箭头函数一样。 In ES6 module code the value of this in the outermost context is defined to be undefined so Babel is simply inlining this value. 在ES6模块代码中,最外层上下文中的this的值被定义为undefined因此Babel只是内联该值。 What you want to do is this: 您要做的是:

var App = React.createClass({
  handleForm(e) {
    e.preventDefault();
  },
  render() {
    return (
        <form>
            <button type="submit" onClick={this.handleForm}>Go</button>
        </form>
    );
  }
});

As a final note, you do not need to bind any member functions when using React.createClass as React will do this for you automatically and in a more efficient way than with Function.prototype.bind . 最后一点,当您使用React.createClass时,您不需要绑定任何成员函数,因为React会比Function.prototype.bind更加自动高效地为您完成此操作。

Thank you @loganfsmyth for the update about why this is undefined . 感谢您@loganfsmyth有关为什么更新thisundefined

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

相关问题 在 React Typescript Firebase 项目上未定义导航器 - Navigator undefined on React Typescript Firebase project 无法在React项目中读取未定义的属性“值”? - Cannot read property 'value' of undefined in react project? Webpack + Typscript 库导入在 React 项目中未定义 - Webpack + Typscript library import is undefined in React project typeof variable !== &#39;undefined&#39; 将变量转换为字符串? - typeof variable !== 'undefined' converts a variable to a string? React_dom_development 在 Deno React 项目中未定义 - react_dom_development is undefined in Deno React project 在React项目中导入Mousetrap - 无法读取undefined的属性&#39;bind&#39; - Importing Mousetrap in React project - Cannot read property 'bind' of undefined 如何修复 React 项目中的“TypeError:无法读取未定义的属性‘继承’”? - How to fix “TypeError: Cannot read property 'inherits' of undefined” in React project? 为什么在 Codecademy React 项目中未定义 jsonResponse 中的属性“id”? - Why is property 'id' in jsonResponse undefined in Codecademy React project? 我在使用 React 制作的项目中遇到未定义的错误 - I'm getting an undefined error in my project made with React 在我的反应原生项目中显示 object 和 axios 给出未定义 - Display object with axios in my react native project gives undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM