Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Return a Page by ID with react-routing?

I'm just new to React and I'm having a hard time figuring out how to return a new page (a component) as I clicked the View button. I have a user table and per row has a View button. I can only retrieve the data in exactly the same page but I would like to retrieve my view details in another page with its id on the url, for example: http://user-details-1. Can someone help me with this. Thanks!

Here's my view code/component:

import React, { useState, useEffect } from "react";
import Form from "react-bootstrap/Form";

const ViewUserDetaiils = (props) => {
  const [user, setUser] = useState(props.currentUser);

  useEffect(() => {
    setUser(props.currentUser);
  }, [props]);

  return (
    <Form>
      <div>
        <div>
          <strong>Id:</strong> {user.id}{" "}
        </div>
        <div>
          <strong>Name:</strong> {user.name}{" "}
        </div>
        <div>
          <strong>Contact:</strong> {user.contact}{" "}
        </div>
        <div>
          <strong>Email:</strong> {user.email}{" "}
        </div>
      </div>
    </Form>
  );
};

export default ViewUserDetails;

Here's my routing:

import React, { Component } from "react";
import { BrowserRouter, Route, Switch } from "react-router-dom";
import Home from "./containers/Home/Home";
import ViewUserDetails from "./forms/ViewUserDetails";
import PageNotFound from "./page404/page404";

class App extends Component {
  render() {
    return (
      <BrowserRouter>
        <div>
          <Switch>
            <Route exact path="/" component={Home} />
            <Route exact path="/home" component={Home} />
            <Route
              exact
              path="/view-contact-details/"
              component={ViewUserDetails}
            />
            <Route component={PageNotFound} />
          </Switch>
        </div>
      </BrowserRouter>
    );
  }
}

export default App;

And here's my code for the View button which is in another component(UserTable component).:

             <Link to="/view-contact-details">
               <Button
                  onClick={() => {
                    props.viewRow(user);
                  }}
                  className="Button muted-Button"
                >
                  View
                </Button>
              </Link>
like image 609
Xev Avatar asked Dec 23 '22 17:12

Xev


1 Answers

Your route must include a slug (the last part of the URL, in this case the user id) to route it dynamically.

<Route
  exact
  path="/view-contact-details/:id"
  component={ViewUserDetails}
/>

Then in your component's Link you pass an object to attribute to. It accepts a pathname (the path with slug/id included) and state (this contains your state/data).

<Link
  to={{
    pathname: `/view-contact-details/${user.id}`,
    state: { users: user }
  }}
>
  <button>View</button>
</Link>;

Finally, in your ViewUserDetails component, you can use useLocation to get the state passed in Link.

import React from "react";
import { useLocation, Link } from "react-router-dom";
import Form from "react-bootstrap/Form";

const ViewUserDetails = _ => {
  const { state } = useLocation();

  return (
      <Form>
        <div>
          <div>
            <strong>Id:</strong> {state.users.id}{" "}
          </div>
          <div>
            <strong>Name:</strong> {state.users.name}{" "}
          </div>
        </div>
      </Form>
  );
};

export default ViewUserDetails;

Here is a working demo in case you want to check it out.

like image 98
Nicolas Hevia Avatar answered Dec 26 '22 00:12

Nicolas Hevia