当前位置: 首页 > 面试题库 >

React.js-无法读取未定义的属性

裴欣荣
2023-03-14
问题内容

我正在制作非常简单的react应用。但是,当我尝试通过onChange事件调用父(实际上是祖父母)组件的方法时,我一直在获取Uncaught TypeError: Cannot read property 'props' of undefined

这是触发事件的组件/表单(因此,在绑定的父组件上调用方法…是的,因为我通过道具将其从父组件传递下来,所以在方法上使用了.bound(this)。)

class MonthsTable extends Component {
  handleChangeOnMonth(e){ 
    this.props.setMonth(e.target.id, e.target.value); // here the method is not found, causing error.
  }
  render(){
    console.log(this.props.setMonth) // here the method is present alright
    return (<form>
      {this.props.months.map((e, i) =>
         <input
          type='number'
          id={i} 
          key={i} // yes I know, bad habit, but in this case it doesn't matter
          value={this.props.months[i]}
          onChange={this.handleChangeOnMonth} />)}
    </form>)
  }
}

这是我如何通过大多数父(祖父母)组件中的props传递该方法的方法。

<Months setMonth={this.setMonth.bind(this)} />

这是我作为父项(方法所有者和方法调用程序之间的组件)中的props传递方法的方式

<MonthsTable setMonth={this.props.setMonth} />

最后传递给您首先看到的组件(MonthsTable)。无论是否相关,最终(大多数子级)组件都会根据if语句是否正常运行而显示(可能以某种方式具有相关性,我不知道)。

问题是…(setMonth)方法为什么在(handleChangeOnMonth)方法内部是“不可见的”。

感谢您的任何建议。


问题答案:

这里的实际问题是this上下文未在您的handleChangeOnMonth函数中定义。这是由于html" target="_blank">javascript处理函数上下文的方式所致,基本上是在调用函数时,如果您不是直接从对象中调用它们,并且未绑定它们,则它们将没有定义的上下文,并且因为您正在传递函数作为输入组件的参数,它将丢失上下文。

解决此问题的最简单方法是绑定函数,建议您在构造函数中绑定函数,如下所示:

class MonthsTable extends Component {
  constructor(props, context){
    super(props, context);
    this.handleChangeOnMonth = this.handleChangeOnMonth.bind(this);
  }
  handleChangeOnMonth(e){ 
    this.props.setMonth(e.target.id, e.target.value);
  }
  render(){
    return (<form>
      {this.props.months.map((e, i) =>
         <input
          type='number'
          id={i} 
          key={i} 
          value={this.props.months[i]}
          onChange={this.handleChangeOnMonth} />)}
    </form>)
  }
}

或者,如果您使用的是装饰器,则可以使用core-decorators包以更优雅的方式执行此操作:

import {autobind} from "core-decorators"

@autobind
class MonthsTable extends Component {     
  handleChangeOnMonth(e){ 
    this.props.setMonth(e.target.id, e.target.value);
  }
  render(){
    return (<form>
      {this.props.months.map((e, i) =>
         <input
          type='number'
          id={i} 
          key={i} 
          value={this.props.months[i]}
          onChange={this.handleChangeOnMonth} />)}
    </form>)
  }
}


 类似资料:
  • 我正在用onsenui和react列清单。但我不能从onchanged调用绑定。 我不明白。。。。有人能解决这个问题吗? 这是我的密码。我想从输入项调用handlechanged方法。但是,无法读取未定义的属性“bind”被引发。

  • 为什么我得到这个错误不能读取未定义的触摸属性? 为什么它不能读取,但它可以读取 当我们使用

  • 我正在测试发送电子邮件与流星js和nodemailer插件: 流星添加捷运:流星NodeEmailer 当页面加载时,我在导航器的控制台上看到错误:无法读取未定义的属性“创建运输”。 那么问题是什么呢? 代码如下: /////////////////////////////////////////// ///////////////

  • 我知道这方面还有很多类似的问题,但没有一个答案是有效的。在我的例子中,我有下面的代码,我正在尝试按分数降序排列记录数组。 最后一个函数是什么给我的问题.如果用户运行以下: var web=[{url:“www.southanpton.ac.uk”,内容:“南安普敦大学提供学位课程和世界一流的研究测试。”,{url:“www.xyz.ac.uk”,内容:“另一种大学考试”},{url:“www”,内

  • 我一直试图让firebase与react一起工作,但出现了此错误 未经处理的拒绝(TypeError):无法读取未定义的fetch D:/Peti/Programming/node/coursewebpage/src/components/coursePage.js:12 9 | const db=firebase.firestore()10 | const myAuthLevel=(fireba

  • 这是我的猫鼬模式: 这是使用Express的Node.js服务器。它具有路由,如果触发该路由,它将使用请求中传递的信息更新用户的购物车。正文: 我打印到终端userCart,正如您在代码中看到的,它返回给我的是: 当服务器执行时,它返回以下错误: 如果我已经在Mongoose中定义了的结构,为什么不定义它?