0

I am trying to create a clickable table, in row manner. When the row is clicked, I would like to open a page and passing the data. Data from the row. At the moment the click works as the console prints. However, I am unable to pass the data and open the page.

Can someone help or guide me how to achieve this?

When I click the row, I can see on console the output. However, it doesn't go to the /flight-detail and pass the data.

FlightList.js

<tbody>
    {flights.map((f, i) => {
        return (
            <tr key={i} onClick={() => handleClick(f)}>
            <td>{f.date}</td>
            <td>{f.flightNumber}</td>
            <td>{f.origin}</td>
            <td>{f.destination}</td>
            </tr>
        );
    })}
</tbody>

Click handler method:

function handleClick(e) {
    // THIS PRINTS AND WORKS
    console.log("clicked", e.flightNumber, "...", e.origin); 

    <Link to={{ pathname: "/flight-detail", data: e }}></Link>;
}

App.js

    ...
    ...
    <Route path="/flight-detail" component={FlightDetail} />
pensum
  • 980
  • 1
  • 11
  • 25
Daniel
  • 478
  • 3
  • 16
  • 32

1 Answers1

0

You can send data with state variable

function handleClick(e) {
  console.log("clicked", e.flightNumber, "...", e.origin); // IT PRINTS AND WORKS
  <Link
    to={{
      pathname: "/flight-detail",
      state: {
        data: e
      }
    }}
  ></Link>;
}
...

class FlightDetail extends React.Component {
  state = {
    data: null,
    ...
  }
  componentDidMount () {
    const { data } = this.props.location.state;
    this.setState({ data: data });
    ...
  }
  render() {
    ...
  }
}
Gary Chen
  • 248
  • 2
  • 14