Skip to content

Commit

Permalink
add: 补充 useSearchParams 用法
Browse files Browse the repository at this point in the history
  • Loading branch information
黄振敏 committed Sep 10, 2024
1 parent 2664490 commit 2d96030
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions docs/6.react.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ const App: React.FC<Props> = ({
export default withRouter(App);
```

**react-router v5**
**react-router v5(推荐)**

任意组件可以采用 `react-route` 提供的 hooks 获取路由参数信息,主要有以下几个 hooks:

- useHistory
- useLocation
- useParams
- useRouteMatch
- **useHistory**: 访问 history 对象,进行编程式导航(如 push 或 replace 路由)。
- **useLocation**: 返回当前的 location 对象,表示应用的当前位置,通常用于获取当前 URL、路径名、状态等信息。
- **useParams**: 获取当前路由的 URL 参数,常用于从 URL 中提取动态值。
- **useRouteMatch**: 匹配当前 URL 与某个特定路径,类似于 v5 中的 match 对象,可以用于自定义路径匹配。

```tsx
import {useHistory, useLocation, useParams, useRouteMatch} from 'react-router';
Expand All @@ -121,6 +121,20 @@ const App: React.FC<Props> = () => {
</button>
);
};
```
因为 `useLocation` 没有暴露出 `query` 的参数类型,因此在 apaas 包中定义了一个 `useSearchParams` 的 hook,用于获取 query 参数。
```ts
import useSearchParams from 'hzero-front-apaas/lib/hooks/useSearchParams';

interface Props {
}

const App: React.FC<Props> = () => {
const {id} = useSearchParams<{id: string}>();

return id
};

```

:::tip
Expand Down

0 comments on commit 2d96030

Please sign in to comment.