我是 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>