react点击按钮跳转详情页
时间: 2023-05-31 20:04:07 浏览: 100
ReactNative页面跳转Navigator实现的示例代码
要实现React点击按钮跳转详情页,可以使用React Router来实现。首先,需要在项目中安装React Router:
```
npm install react-router-dom
```
然后,在需要跳转的组件中,引入React Router:
```jsx
import { useHistory } from "react-router-dom";
```
在组件中定义一个函数,用于处理点击事件:
```jsx
const handleClick = () => {
history.push("/detail"); // 跳转到详情页
};
```
最后,在组件中渲染一个按钮,并绑定点击事件:
```jsx
<button onClick={handleClick}>跳转到详情页</button>
```
完整代码示例:
```jsx
import React from "react";
import { useHistory } from "react-router-dom";
const MyComponent = () => {
const history = useHistory();
const handleClick = () => {
history.push("/detail"); // 跳转到详情页
};
return (
<div>
<h1>My Component</h1>
<button onClick={handleClick}>跳转到详情页</button>
</div>
);
};
export default MyComponent;
```
阅读全文