3

我正在尝试在 React-table 中的 onclick 函数内进行重定向。当您查看 onRowClick 时,它会记录完美运行的标题名称。但是当我在点击后尝试使用重定向时,什么也没有发生。该代码在我在不同的组件中尝试后有效。我想知道这是否是 react-table 的问题,是否还有其他方法可以做到这一点?

我想要做的是 onclick 重定向到附加有 Patientinfo 组件的路由。

import React, { Component } from 'react';
import { highRiskPatientData } from     '.././dummydata/HighRiskPatientComp';
import '.././styles/HighRiskPatientComp.css';
import ReactTable from 'react-table/lib';
import "react-table/react-table.css";
import "babel-polyfill";
import PatientInfo from './PatientInfo';
import { Redirect } from 'react-router-dom';


class HighRiskPatientComponent extends Component {
  // constructor() {
  //   super();

  // }

  render() {
    const onRowClick = (state, rowInfo, column, instance) => {
      return {
          onClick: e => {
              console.log('It was in this column:', column.Header);
              <Redirect to="/patientinfo"/>
            }
      }
  }
    return (
  <div className="container-fluid">
    <div className="row">
      <div className="col-md-6 HighRiskHeaderDiv">
        <h5 className="HighRiskHeader">High Risk Patients</h5>
          </div>
          <div className="col-md-6 btnSettingsDiv">
            <button type="button" className="btn btnSettings"         
 value="Submit">Settings</button>
          </div>
        </div>

    <ReactTable
      data={highRiskPatientData.data}
      columns={[
             {

               columns: [
                 {
                   Header: "Name",
                   accessor: "name"
                 },
                 {
                   Header: "Current PRF Status",
                   accessor: "currentPRFStatus"
                 },
                 {
                   Header: "Review On",
                   accessor: "reviewOn"
                 },
                 {
                   Header: "Due/Overdue",
                   accessor: "DueOverdue"
                 },
                 {
                   Header: "Facility",
                   accessor: "Facility"
                 },
                 {
                   Header: "Last Discharge",
                   accessor: "lastDischarge"
                 },
                 {
                   Header: "Last MHA Appt",
                   accessor: "lastMHAppt"
                 },
                 {
                   Header: "Next MHA Appt",
                   accessor: "nextMHAppt"
                 },
                 {
                   Header: "On Track",
                   accessor: "onTrack"
                 },
                 {
                   Header: "Last Safety Plan",
                   accessor: "lastSafetyPlan"
                 },
                 {
                   Header: "Done in 7 Days",
                   accessor: "doneIn7Days"
                 },
                 {
                   Header: "Last Compreh. Risk Assess",
                   accessor: "lastCompRiskAssess"
                 },
                 {
                   Header: "PHQ-9 Score",
                   accessor: "phqDash9Score"
                 },
                 {
                   Header: "PHQ-9 date",
                   accessor: "phqDash9Score"
                 },
                 {
                   Header: "Trend",
                   accessor: "trend"
                 }
               ]
             }
           ]}
      defaultPageSize={8}
      className="-highlight"
      getTdProps={onRowClick}
        />
      </div>
    );
  }
}

export default HighRiskPatientComponent;
4

2 回答 2

0

您有 2 个选项来实现这一目标:

使用历史对象

onClick: e => {
    console.log('It was in this column:', column.Header);
    this.props.history.push("/patientinfo");
}

使用重定向组件。如果你想使用redirect组件,你需要将它包含在你的render函数中,而不是onClick你实现的方法中。但是为了知道何时渲染它(实际重定向),您需要在单击行时更改状态,如下所示:

onClick: e => {
    console.log('It was in this column:', column.Header);
    this.setState({ navigatePatientInfo: true });
}

然后在您的渲染方法中:

render() {
    if (this.state.navigatePatientInfo) {
        return <Redirect to="/patientinfo"/>
    }

    // .. your code
}
于 2019-07-16T09:00:02.190 回答
0

我失去了一些时间来弄清楚。我的答案是基于这个答案。我不知道这是否是最好的解决方案,但我正在使用 SubComponent 并在那里使用了 Redirect。不要忘记“expand_row”函数和“expanded={this.state.expanded}”。

getTdProps={(state, rowInfo) => {
        return {
          onClick: e => {
            this.expand_row(rowInfo);
          }
        };
      }}
      SubComponent={d => {
        return (
          <div>
            <Redirect
              to={{
                pathname: "transaction",
                accountId: d.original._id
              }}
            />
          </div>
        );
      }}
于 2019-07-16T08:40:53.007 回答