ReactJS props.func 不是函数

发布于 2025-01-10 21:40:45 字数 3739 浏览 1 评论 0原文

我发现了许多与此类似的问题以及这些问题的答案,但我还没有找到我已经弄清楚如何应用于我的问题的答案。该应用程序加载良好并显示表单,但在提交时不会创建表格。我仍在学习如何有效地使用道具,所以我不确定我哪里出了问题。

当我尝试提交表单时出现以下错误;

Uncaught TypeError: props.func is not a function
    transferValue AddCombatantTable.jsx:33
    React 14
    unstable_runWithPriority scheduler.development.js:468
    React 15
    js index.js:7
    factory react refresh:6
    Webpack 3

我正在尝试创建一个基于 这个

这是AddCombatantTable.jsx;

import React, { useState } from 'react';
  
function CombatantForm(props) {
  const [initiative, setInit] = useState('');
  const [name, setName] = useState('');
  const [armorClass, setAC] = useState('');
  const [hitPoints, setHP] = useState('');
  
  const changeInit = (event) => {
    setInit(event.target.value);
  };
  
  const changeName = (event) => {
    setName(event.target.value);
  };
  
  const changeAC = (event) => {
    setAC(event.target.value);
  };

  const changeHP = (event) => {
    setHP(event.target.value);
  };
  
  const transferValue = (event) => {
    event.preventDefault();
    const val = {
      initiative,
      name,
      armorClass,
      hitPoints,
    };
    props.func(val);
    clearState();
  };
  
  const clearState = () => {
    setInit('');
    setName('');
    setAC('');
    setHP('');
  };
  
  return (
    <div>
      <label>Initiative</label>
      <input type="text" value={initiative} onChange={changeInit} />
      <label>Name</label>
      <input type="text" value={name} onChange={changeName} />
      <label>Armor Class</label>
      <input type="text" value={armorClass} onChange={changeAC} />
      <label>Hit Points</label>
      <input type="text" value={hitPoints} onChange={changeHP} />
      <button onClick={transferValue}>Add Combatant</button>
    </div>
  );
}
  
export default CombatantForm;

AddCombatantForm.jsx;

import React, { useState } from 'react';
import CombatantForm from './AddCombatantForm';
import jsonData from './data.json';
  
function TableData() {
  const [combatantData, setCombatantData] = useState(jsonData);
  
  const tableRows = combatantData.map((info) => {
    return (
      <tr>
        <td>{info.id}</td>
        <td>{info.initiative}</td>
        <td>{info.name}</td>
        <td>{info.armorClass}</td>
        <td>{info.hitPoints}</td>
      </tr>
    );
  });
  
  const addRows = (data) => {
    const totalCombatants = combatantData.length;
    data.id = totalCombatants + 1;
    const updatedCombatantData = [...combatantData];
    updatedCombatantata.push(data);
    setCombatantData(updatedCombatantData);
  };
  
  return (
    <div>
      <table className="table table-stripped">
        <thead>
          <tr>
            <th>ID</th>
            <th>Initiative</th>
            <th>Name</th>
            <th>Armor Class</th>
            <th>Hit Points</th>
          </tr>
        </thead>
        <tbody>{tableRows}</tbody>
      </table>
      <CombatantForm func={addRows} />
    </div>
  );
}
  
export default TableData;

应用程序.js;

import TableData from './components/AddCombatantTable';

function App() {  
  return (
    <div className="App">
      <TableData />
    </div>
  );
}

export default App;

I have found many questions similar to this and answers to those questions, but I haven't found an answer that I have figured out how to apply to my problem yet. The app loads fine and displays the form, but doesn't create a table upon submit. I'm still learning how to use props effectively so I'm not sure where I'm going wrong yet.

I get the following error when I try to submit the form;

Uncaught TypeError: props.func is not a function
    transferValue AddCombatantTable.jsx:33
    React 14
    unstable_runWithPriority scheduler.development.js:468
    React 15
    js index.js:7
    factory react refresh:6
    Webpack 3

I am trying to create a form that creates a table based on this.

Here is AddCombatantTable.jsx;

import React, { useState } from 'react';
  
function CombatantForm(props) {
  const [initiative, setInit] = useState('');
  const [name, setName] = useState('');
  const [armorClass, setAC] = useState('');
  const [hitPoints, setHP] = useState('');
  
  const changeInit = (event) => {
    setInit(event.target.value);
  };
  
  const changeName = (event) => {
    setName(event.target.value);
  };
  
  const changeAC = (event) => {
    setAC(event.target.value);
  };

  const changeHP = (event) => {
    setHP(event.target.value);
  };
  
  const transferValue = (event) => {
    event.preventDefault();
    const val = {
      initiative,
      name,
      armorClass,
      hitPoints,
    };
    props.func(val);
    clearState();
  };
  
  const clearState = () => {
    setInit('');
    setName('');
    setAC('');
    setHP('');
  };
  
  return (
    <div>
      <label>Initiative</label>
      <input type="text" value={initiative} onChange={changeInit} />
      <label>Name</label>
      <input type="text" value={name} onChange={changeName} />
      <label>Armor Class</label>
      <input type="text" value={armorClass} onChange={changeAC} />
      <label>Hit Points</label>
      <input type="text" value={hitPoints} onChange={changeHP} />
      <button onClick={transferValue}>Add Combatant</button>
    </div>
  );
}
  
export default CombatantForm;

AddCombatantForm.jsx;

import React, { useState } from 'react';
import CombatantForm from './AddCombatantForm';
import jsonData from './data.json';
  
function TableData() {
  const [combatantData, setCombatantData] = useState(jsonData);
  
  const tableRows = combatantData.map((info) => {
    return (
      <tr>
        <td>{info.id}</td>
        <td>{info.initiative}</td>
        <td>{info.name}</td>
        <td>{info.armorClass}</td>
        <td>{info.hitPoints}</td>
      </tr>
    );
  });
  
  const addRows = (data) => {
    const totalCombatants = combatantData.length;
    data.id = totalCombatants + 1;
    const updatedCombatantData = [...combatantData];
    updatedCombatantata.push(data);
    setCombatantData(updatedCombatantData);
  };
  
  return (
    <div>
      <table className="table table-stripped">
        <thead>
          <tr>
            <th>ID</th>
            <th>Initiative</th>
            <th>Name</th>
            <th>Armor Class</th>
            <th>Hit Points</th>
          </tr>
        </thead>
        <tbody>{tableRows}</tbody>
      </table>
      <CombatantForm func={addRows} />
    </div>
  );
}
  
export default TableData;

App.js;

import TableData from './components/AddCombatantTable';

function App() {  
  return (
    <div className="App">
      <TableData />
    </div>
  );
}

export default App;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-黛色若梦 2025-01-17 21:40:45

请尝试一下

props?.func(val)

Please try it

props?.func(val)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文