javascript - React - 动态表中的 OnChange 调用会产生 Uncaught TypeError

标签 javascript reactjs typescript

当前,当创建表及其行的严格要求发挥作用时,我遇到了错误。请注意,我省略了代码中不相关的方面,问题被隔离到将 onChange 添加到表行的输入时。

我不确定为什么会出现这个问题,但我假设这可能与该表是基于映射动态创建的事实有关,并且在创建表行时出现了一些问题,并且我'我缺少某种类型的正确语法来在这种情况下正确调用 onChange?

class SkillTable extends React.Component<any,any>
{
  constructor(props: any) {
    super(props);

    this.state = {
      skillList: [],
      unTrainedList: [],
      classSkill: [],
      playerSkills: [],
    };
    this.handlePopulates = this.handlePopulates.bind(this);
    this.handleSkillChange = this.handleSkillChange.bind(this);
  }


renderTableRows(skill, index)
  {

    return (
      <tr key={index}>
        <td>{skill.unTrainedTrue()}</td>
        <td>{skill.classSkillTrue()}</td>
        <td>{skill.skillName}</td>
        <td>{skill.abilityName}</td>
        <td>{skill.skillTotal}</td>
        <td> <input type="number" value={skill.skillRank} onChange={this.handleSkillChange}/> </td>
        <td> <input type="number" value={skill.abilityMod}/> </td>
        <td> <input type="number" value={skill.raceMod}/> </td>
        <td> <input type="number" value={skill.miscMod}/> </td>
        <td> <input type="number" value={skill.synergyMod}/> </td>
        <td> <input type="number" value={skill.skillCost}/> </td>
      </tr>
    )
  }


  render() {
    return (
      <div>
        <table className="table table-bordered">
          <thead>
            <tr>
              <th>UT</th>
              <th>CS</th>
              <th>Skill Name</th>
              <th>Ability</th>
              <th>Skill Total</th>
              <th>Rank</th>
              <th>Ability Mod</th>
              <th>Race Mod</th>
              <th>Misc Mod</th>
              <th>Synergy Mod</th>
              <th>Skill Cost</th>
            </tr>
          </thead>

          <tbody>
            {this.state.playerSkills.map(this.renderTableRows)}
          </tbody>

        </table>
        </div>
    );
  }
}


  handleSkillChange(evt)
  {
    //LOGIC HERE, BUT CRASH ON CALL
  }

最佳答案

我认为您也应该将 renderTableRows 绑定(bind)到您的组件。示例:

constructor(props: any) {
  super(props);

  this.state = {
    skillList: [],
    unTrainedList: [],
    classSkill: [],
    playerSkills: [],
  };
  this.handlePopulates = this.handlePopulates.bind(this);
  this.handleSkillChange = this.handleSkillChange.bind(this);
  this.renderTableRows = this.renderTableRows.bind(this);
}

关于javascript - React - 动态表中的 OnChange 调用会产生 Uncaught TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61511765/

相关文章:

node.js - 从 Cloud Functions Realtime Database 中的子引用获取父 Node 的原始值 "val()"数据

TypeScript:当匿名对象属性不满足类型规范时强制类型错误

typescript - 类型错误 : Property 'currentTime' does not exist on type 'EventTarget & HTMLInputElement'

javascript - 从提交 Angular 6 获取 HTML 选择值和输入值

Javascript noob - 创建一个 "set"类,代码不工作

javascript - JS - 为什么 Codewars 挑战的一项测试返回未定义,而其他 104 项测试却通过了?

javascript - Ramda(或其他 FP 库)用于选择可能为空键的用法

javascript - Chart.js 极地面积图。将标签更改为 45 度 Angular

javascript - Create React App 出现 "Unexpected labeled statement"警告

Javascript 变量未在 ReactJS 组件中存储值