reactjs - 我如何在反应钩子中使用这个 RouteComponentProps
问题描述
有RouteComponentProps
export interface RouteComponentProps<
Params extends { [K in keyof Params]?: string } = {},
C extends StaticContext = StaticContext,
S = H.LocationState
> {
history: H.History<S>;
location: H.Location<S>;
match: match<Params>;
staticContext?: C;
}
使用状态我会做
class Example extends React.PureComponent<
RouteComponentProps<{ id: string }>,
State
>
我不确定如何将其转换为使用钩子
解决方案
下面是使用 React 钩子和react-router-dom
包的例子。
import { useParams } from "react-router-dom";
//hook example
const Example = () => {
const params = useParams(); //retreive params you need
console.log(params)
return (
<div>
//...other code
</div>
);
}
export default App;
推荐阅读
- delphi - Delphi XE:为什么File->New->Other->ActiveX中的一些项目是灰色的?[解决了]
- operating-system - 超线程可以解决哪些问题?
- php - 使用php将递归xml转换为Json
- javascript - 三.js 怎么解决这个问题?(获取 physijs_worker 404(未找到))
- android - 有没有办法让两个 Android 手表(例如三星)通过 NFC 或蓝牙相互通信?
- c# - 如何在委托函数中使用参数?
- javascript - Reactjs 提交多个表单
- linux - u-boot:尽管内核小于最大 BOOTM_LEN,但无法启动 linux 内核
- python - ATBSWP 第 2 版第 12 章,打开 google 或其他网站的所有搜索结果不起作用
- c# - 如何根据另一张表的 ID(作为外键)插入到一张表中?