Reactjs 无法读取属性';绑定&x27;React.js中未定义的

Reactjs 无法读取属性';绑定&x27;React.js中未定义的,reactjs,Reactjs,我绑定了this.handleChange=this.handleChange.bind(this)在构造函数中,但我仍然得到未定义的无法读取属性 如何解决这个问题 import React, { Component } from "react"; import moment from "moment"; class TimerConfig extends Component { constructor() { super(); this.handleChange = th

我绑定了this.handleChange=this.handleChange.bind(this)在构造函数中,但我仍然得到未定义的无法读取属性

如何解决这个问题

import React, { Component } from "react";
import moment from "moment";

class TimerConfig extends Component {
  constructor() {
    super();
    this.handleChange = this.handleChange.bind(this);
  }

  handChange(ev) {
    const newBaseTime = this.props.baseTime;

    if (ev.target.id === "hours")
      newBaseTime
        .subtract(newBaseTime.get("hour"), "hours")
        .add(parseInt(ev.target.value, 10), "hours");
    if (ev.target.id === "minutes")
      newBaseTime
        .subtract(newBaseTime.get("minutes"), "minutes")
        .add(parseInt(ev.target.value, 10), "minutes");
    if (ev.target.id === "seconds")
      newBaseTime
        .subtract(newBaseTime.get("seconds"), "seconds")
        .add(parseInt(ev.target.value, 10), "seconds");

    this.props.setBaseTime(newBaseTime);
  }

  render() {
    return (
      <div>
        <label for="hours">Hour</label>
        <input
          id="hours"
          type="number"
          defaultValue={this.props.baseTime.get("hours")}
          onChange={this.handleChange}
        />

        <label for="minutes">min</label>
        <input
          id="minutes"
          type="number"
          defaultValue={this.props.baseTime.get("minutes")}
          onChange={this.handleChange}
        />

        <label for="seconds">sec</label>
        <input
          id="seconds"
          type="number"
          defaultValue={this.props.baseTime.get("seconds")}
          onChange={this.handleChange}
        />
      </div>
    );
  }
}

export default TimerConfig;
import React,{Component}来自“React”;
从“时刻”中导入时刻;
类TimerConfig扩展组件{
构造函数(){
超级();
this.handleChange=this.handleChange.bind(this);
}
换手(ev){
const newBaseTime=this.props.baseTime;
如果(ev.target.id==“小时”)
纽巴斯基时代
.减去(newBaseTime.get(“小时”),“小时”)
.add(parseInt(ev.target.value,10),“小时”);
如果(ev.target.id==“分钟”)
纽巴斯基时代
.减去(newBaseTime.get(“分钟”),“分钟”)
.add(parseInt(ev.target.value,10),“分钟”);
如果(ev.target.id==“秒”)
纽巴斯基时代
.减去(newBaseTime.get(“秒”),“秒”)
.add(parseInt(ev.target.value,10),“秒”);
this.props.setBaseTime(newBaseTime);
}
render(){
返回(
时辰
闵
秒
);
}
}
导出默认时间配置;

当你想在
构造函数中绑定
时,你需要使用
super
道具

constructor(props){
  super(props);
  this.handleChange = this.handleChange.bind(this);
}
否则,可以使用以下语法进行绑定:

handChange=(ev)=>{
//..
}

你把你的方法拼错了“handChange”而不是“handleChange”:)

Jimenemex lol。很抱歉。你的方法拼错了,
handChange
如果你想在构造函数中使用
this.props
,你只需要将道具传递给
super
。这不是OP面临的问题。