4

我是 React 的新手,当我单击View按钮时,我很难弄清楚如何返回一个新页面(一个组件)。我有一个用户表,每行都有一个查看按钮。我只能在完全相同的页面中检索数据,但我想在另一个页面中检索我的视图详细信息,其 url 上有其 id,例如:http://user-details-1。有人可以帮我弄这个吗。谢谢!

这是我的视图代码/组件:

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;

这是我的路线:

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;

这是我在另一个组件(UserTable 组件)中的View按钮的代码:

             <Link to="/view-contact-details">
               <Button
                  onClick={() => {
                    props.viewRow(user);
                  }}
                  className="Button muted-Button"
                >
                  View
                </Button>
              </Link>
4

1 回答 1

10

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.

于 2020-08-02T11:39:24.173 回答