简体   繁体   English

反应:_this.state未定义

[英]React: _this.state is undefined

I wrote the following snippet: 我写了以下代码片段:

export class Kurse extends React.Component {
  constructor(props) {
    super(props);

    this.state = {termineFreiburg: [
      {date: "21.02.2019", free: 10}, 
      {date: "02.05.2018", free: 0}, 
      {date: "13.03.2018", free: 10}, 
      {date: "04.07.2018", free: 12}, 
      {date: "05.08.2018", free: 12}
    ],
                termineKarlsruhe:[
      {date: "09.03.2019", free: 2},
      {date: "12.01.2018", free: 5}
    ]}
  }

  stadt = this.props.location.stadt;

  listItemsFreiburg = this.state.termineFreiburg.map((termin) =>
  <p><Link to={{pathname: "/bookCourse", datum: termin.date, stadt: this.props.location.stadt}}><Button>{termin.date}   Freie Plätze:  {(termin.free > 0)?termin.free:'ausgebucht'}</Button></Link></p>);

  listItemsKarlsruhe = this.state.termineKarlsruhe.map((termin) =>
  <p><Link to={{pathname: "/bookCourse", datum: termin.date, stadt: this.props.location.stadt}}><Button>{termin.date}   Freie Plätze:   </Button></Link></p>);

  render() {
    return(
      <div>
        <h2>Wähle Deinen Wunschtermin für einen Kurs in {this.props.location.stadt}</h2>
        {(this.props.location.stadt === 'Freiburg') ? this.listItemsFreiburg : this.listItemsKarlsruhe}
      </div>
    );
  }
}


export default Kurse;

Expected result: 预期结果:

I expect that the dates will appear on the website (the app page). 我希望这些日期会出现在网站(应用程序页面)上。

Actual result: 实际结果:

I get "_this.state is not defined" with "constructor(props)" being highlighted. 我得到“未定义_this.state”,突出显示了“ constructor(props)”。

As soon as I take the snippet with the dates out of the constructor all works fine!? 一旦我将带有日期的代码片段从构造函数中取出,一切正常!

Any help? 有什么帮助吗? Or hints? 或提示?

Class field initializers like the one you're using to create listItemsFreiburg and such are executed before the code in the constructor (they're effectively inserted into the constructor at the beginning after the call to super ). 类字段初始值设定项(例如用于创建listItemsFreiburg初始值设定项)在构造函数中的代码之前执行(将它们有效地插入到构造函数中,调用super之后的开头)。 Eg, this: 例如:

class Example {
    constructor(...args) {
        super(...args);
        this.answer = 42;
    }

    question = "Life, the Universe, and Everything";
}

is effectively this: 实际上是这样的:

class Example {
    constructor(...args) {
        super(...args);
        this.question = "Life, the Universe, and Everything";
        this.answer = 42;
    }
}

Notice the order. 注意订单。 Details in the class fields proposal . 课程领域提案详细信息。

That's why state is undefined as of when you're calling map to create listItemsFreiburg and the other one. 这就是为什么在调用map创建listItemsFreiburg和另一个时, state undefined的原因。

Either use a field initializer to create your state, or move your class field initializers into the constructor. 使用字段初始化器创建状态,或将类字段初始化器移到构造函数中。

However , it seems odd to build those at construction time, given that they're based on state. 但是 ,鉴于它们是基于状态的,因此在构建时构建它们似乎很奇怪。 I'd move them into render as locals, or at least update them when state changes. render它们作为本地对象移入render ,或者至少在状态更改时更新它们。

listItemsFreiburg

listItemsKarlsruhe

stadt = this.props.location.stadt;

These need to be in the render() { return() } above the return statement. 这些必须位于return语句上方的render() { return() }

like this with const before them. 像这样在他们之前加上const

render() {
 const listItemsFreiburg =  this.state.termineFreiburg.map((termin) =>
  <p><Link to={{pathname: "/bookCourse", datum: termin.date, stadt: this.props.location.stadt}}><Button>{termin.date}   Freie Plätze:  {(termin.free > 0)?termin.free:'ausgebucht'}</Button></Link></p>);
 const { stadt } = this.props.location;
 return (...) 
}

You should bind listItemsFreiburg & listItemsKarlsruhe to this in the constructor like this: 你应该绑定listItemsFreiburg&listItemsKarlsruhe 这个在这样的构造:

constructor(props) {
   super(props);

   this.state = {
      // Your data
   }

   this.listItemsFreiburg = this.listItemsFreiburg.bind(this);
   this.listItemsKarlsruhe = this.listItemsKarlsruhe.bind(this);

}

You don't need contructor(props) and super(props) just if you need to use this props in constructor. 即使您需要在构造函数中使用此props,也不需要contructor(props)和super(props)。

You can't render just an object if the conditions is true. 如果条件为真,则不能仅渲染对象。

import React, { Component } from "react";
import { render } from "react-dom";

class App extends Component {
  state = {
    termineFreiburg: [
      { date: "21.02.2019", free: 10 },
      { date: "02.05.2018", free: 0 },
      { date: "13.03.2018", free: 10 },
      { date: "04.07.2018", free: 12 },
      { date: "05.08.2018", free: 12 }
    ],
    termineKarlsruhe: [
      { date: "09.03.2019", free: 2 },
      { date: "12.01.2018", free: 5 }
    ]
  };

  renderFreiburg = () => {
    return this.state.termineFreiburg.map(item => {
      return (
        <p>
          <span>{item.date} | </span>
          <span>{item.free}</span>
        </p>
      );
    });
  };
  render() {
    return <div>{true ? this.renderFreiburg() : null}</div>;
  }
}

render(<App />, document.getElementById("root"));

here is my example: https://codesandbox.io/s/q8kxr898l9 这是我的示例: https : //codesandbox.io/s/q8kxr898l9

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

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