代码之家  ›  专栏  ›  技术社区  ›  JS3

如何启用行内单击并重定向到包含所选行以下数据的新选项卡?

  •  0
  • JS3  · 技术社区  · 4 年前

    我正在使用mui数据表,如何通过单击一行将用户重定向到另一个页面,并且该行的数据也将传递到该页面上?

    这是我得到的错误:

    DataCloneError: Failed to execute 'pushState' on 'History': Symbol(react.element) could not be cloned.
    
      constructor() {
        super();
        this.state = { array: []};
      }
    
      columns = [
        "Order ID",
        "Name",
      ];
      options = {
        filter: true,
         onRowClick: (data) => this.props.history.push("/details", data),
      };
    
      componentDidMount() {
        try {
        //fetching data from the firestore
        } catch (err) {
          console.log(err);
        }
      }
    
      render() {
        
        return  (
          <div>
            <MUIDataTable
              title={"Pending Orders"}
              columns={this.columns}
              data={this.state.orders}
              options={this.options}
            />
            
    
    0 回复  |  直到 4 年前
        1
  •  0
  •   Priyansh Gaharana    4 年前

    您可以使用以下命令使用行Data重定向用户 使用历史 钩子。history.push接受path和state(用户想要传递给新路由的数据)作为参数。

    /*** BasicTable.js Line number 35 in the sandbox link***/
    
    const history = useHistory();
    // rowData is the data of the clicked row.
    const handleRowClick = (rowData) => {
       history.push("/nextpage", rowData);
    };
    

    要提取状态,可以使用Location钩子。

     const location = useLocation();
     const rowData = location.state;
    

    Sandbox Url