javascript - ReactJs 如何从不同页面向表添加行

标签 javascript reactjs

我正在制作一个应用程序,其中一页上有一个表格,我需要通过单击按钮从不同页面向该表格添加一行。我的页面使用reactstrap和reactjs,我已经导出了所有页面,并且可以通过单击按钮来呈现它们。

表格页面示例:文件名table.js

class table extends Component {
  render() {
    return (
            <table>
            <thead>
              <tr>
                <th>row1</th>
                <th>row2</th>
                <th>row3</th>
                <th>row4</th>
                <th>row5</th>
              </tr>
            </thead>

            <tbody>
                <tr>
                  <td scope="row">1</td>
                  <td>text</td>
                  <td>text</td>
                  <td>text</td>
                  <td>text</td>
                </tr>
            </tbody>
)
};
export default table;

我需要此页面的代码,例如 - example.js

class addRow extends Component {
  I need the function that will add a row to table.js on button click

   render() {
     return(
        <Button>this button will add a row to table.js</Button>
     )
   };
 }
 export default addRow;

在此页面上,我需要将代码放入 table.js 页面上的行。 它本质上会向表中添加一行,从 example.js 到 table.js

是否有人可以将我联系到或解释我如何做到这一点?

最佳答案

如果 AddRow 旨在成为 Table 的子组件,那么您可以将方法作为 prop 向下传递,如下所示:

class Table extends Component {
    addRow = () => {
        // code to add row to table
    }
    render() {
        return (
            <table> ... </table>
            <AddRow addRow={this.addRow}/>
        )
    };
}

class AddRow extends Component {
    render() {
        return (
            <button onClick={this.props.addRow}>Add Row</button>
        )
    }
}

关于javascript - ReactJs 如何从不同页面向表添加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51459786/

相关文章:

javascript - 如何使用 jquery 动态更改 wordpress 网站中 div 的背景?

reactjs - 更新 react 组件而无需其外部的父组件

json - 从 API react 输出 JSON

reactjs - React router No Match 404 部署时不工作?

javascript - 让复选框过滤器正常工作

javascript - 使用 jquery 按值选择多选下拉列表

javascript |对象分组

javascript - jQuery Mobile click event.preventDefault 似乎并没有阻止更改

css - 如何将CSS应用于标签的属性?

javascript - 为什么我无法读取事件中的属性值