简体   繁体   English

目标未定义的React.js,获取数据课程

[英]Target undefined React.js, fetch data lesson

Im following 30 days of react, day 16: https://www.fullstackreact.com/30-days-of-react/day-16/ 我在30天的反应之后(第16天): https//www.fullstackreact.com/30-days-of-react/day-16/

Having it localy I have this error whenever I make any changes to form or time zone: 将其设置为本地时,每当我对表单或时区进行任何更改时,都会出现此错误:

TypeError: Cannot read property 'target' of undefined

TimeForm._changeMsg
C:\Users\user\Desktop\predica-react\src\TimeForm.js:28

  25 |  }
  26 | 
  27 |  _changeMsg(evt) {
> 28 |    const msg =
  29 |      encodeURIComponent(evt.target.value).replace(/%20/, '+');
  30 |    this.setState({msg}, this._handleChange);
  31 |  }

Also there were wrong bindings in Timeform.js(threw errow) I've set them to 另外在Timeform.js中有错误的绑定(抛出错误),我将它们设置为

this._handleFormSubmit = this._handleFormSubmit.bind(this);
this._changeMsg = this._changeMsg.bind(this);
this._changeTimezone = this._changeMsg.bind(this);
this._handleChange = this._changeMsg.bind(this);

Apart from that everything is exactly as writen in the tutorial. 除此之外,所有内容都与本教程中的内容完全相同。

What should I do to make it work? 我应该怎么做才能使其正常工作?

Edit1: Here is the requested jsx code(Timeform.js): Edit1:这是请求的jsx代码(Timeform.js):

return (
  <form onSubmit={this._handleFormSubmit}>
    <select
      onChange={this._changeTimezone}
      defaultValue={tz}>
      {timezones.map(t => {
        return (<option key={t} value={t}>{t}</option>)
      })}
    </select>
    <input
      type="text"
      placeholder="A chronic string message (such as 7 hours from now)"
      onChange={this._changeMsg}
    />
    <input
      type="submit"
      value="Update request"
    />
  </form>
)

Edit2: I've cloned official repo and it appears to have no bindings at all in timezome.js and it works https://github.com/fullstackreact/30-days-of-react/tree/master/day-16 Edit2:我已经克隆了官方仓库,它在timezome.js中似乎完全没有绑定,并且可以正常工作https://github.com/fullstackreact/30-days-of-react/tree/master/day-16

Edit3: The bindings are there, but moved from constructor to jsx. Edit3:绑定在那里,但是从构造函数移到了jsx。

我要做的就是将绑定从构造函数移动到jsx,并关闭大小写。

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

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