【问题标题】:React.js Material-Table pass selected rows to another pageReact.js Material-Table 将选定的行传递到另一个页面
【发布时间】:2021-01-25 17:11:54
【问题描述】:

如何通过传递选定的行数据作为道具重定向到另一个页面?

我正在使用材料表,我想在单击“导出”按钮后将选定的行数据传递到另一个页面,以便我可以使用该数据在另一个页面中创建某种报告。

我认为我应该使用 history.push() 方法,但它在 onClick 方法中不起作用。有人可以给我任何提示吗?

import React from 'react'
import MaterialTable from 'material-table';

class LeadTable extends React.Component{
    constructor(props) {  
      super(props);
      this.state = { 
        leads : [],
      };
    }
  
    componentDidMount() {
      fetch('http://localhost:5000/api/Leads')
      .then(res => res.json())
      .then((data) => {
        // console.log('Data: ', data[0])
        this.setState({
          leads: data[0]
        })
      })
      .catch(console.log);
    }

    redirectToReport = () => {
      const { history } = this.props;
      history.push('report');
    }

    render(){
      return (
        <div style={{ maxWidth: '100%' , align: 'center'}}>
          <MaterialTable
            title="Reporting"
            columns={[
              ...
            ]}
            data = {this.state.leads}       
            options={{
              selection: true,
              filtering: true,
              sorting: true
            }}
            actions = {[{
              position: "toolbarOnSelect",
              
              tooltip: 'Export the selected activities!',
              icon: 'Export',
  
              onClick: (event, rowData) => {
                console.log("Row Data: " , rowData)
                // rowData has all the selected row and I want to redirect to another page with passing those data.
              }
            }]}
          />
        </div>
    )}
}

export default LeadTable

【问题讨论】:

    标签: javascript reactjs material-ui material-table


    【解决方案1】:

    此答案主要针对使用类组件的 OP 代码库。如果你使用函数组件可以使用react-router hooksuseHistory


    使用withRouter HOC 启用LeadTable 组件访问history,这样你就可以push

    const LeadTableWithRouter = withRouter(LeadTable);
    

    将对象传递给push函数以传递行数据

    redirectToReport = (rowData) => {
      const { history } = this.props;
      history.push({
        pathname: "/report", // re-route to this path
        state: { name: rowData.name, surname: rowData.surname } // your row data
      });
    };
    

    在您的其他组件中,使用this.props.location.state.&lt;data_name&gt; 访问您传递的行数据

    class AnotherPage extends React.Component {
      render() {
        return (
          <>
            <p>{this.props.location.state.name}</p>
            <p>{this.props.location.state.surname}</p>
            <Link to="/">go back</Link>
          </>
        );
      }
    }
    

    【讨论】:

    • 谢谢!但是我不能在底部的 onClick 方法中调用redirectToReport 函数。错误说'redirectToReport 没有定义。
    • 这样称呼它:this.redirectToReport,因为你在一个班级里。看看codesandbox.io/s/practical-field-xoc8y?file=/src/…
    猜你喜欢
    • 2013-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 2019-05-09
    • 2021-08-17
    相关资源
    最近更新 更多