Skip to content

Commit

Permalink
docs(useUrlState): update docs and test case for multi-state manageme…
Browse files Browse the repository at this point in the history
…nt (#2125)

* feat: useUrlState can handle multiple useUrlState updates simultaneously

* fix: change text

* fix: do not use promise

---------

Co-authored-by: miracles1919 <516571350@qq.com>
Co-authored-by: 云泥 <1656081615@qq.com>
  • Loading branch information
3 people committed Apr 7, 2023
1 parent 4f293c3 commit 0fb401e
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 11 deletions.
85 changes: 85 additions & 0 deletions packages/use-url-state/demo/demo4.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* title: Multi-state management (split)
* desc: useUrlState can handle multiple useUrlState updates simultaneously
*
* title.zh-CN: 多状态管理 (拆分)
* desc.zh-CN: useUrlState 可以同时处理多个 useUrlState 更新
*/

import React from 'react';
import useUrlState from '@ahooksjs/use-url-state';

export default () => {
const [page, setPage] = useUrlState({ page: '1' });
const [pageSize, setPageSize] = useUrlState({ pageSize: '10' });

return (
<>
<div>
page: {page.page}
<span style={{ paddingLeft: 8 }}>
<button
onClick={() => {
setPage((s) => ({ page: Number(s.page) + 1 }));
}}
>
+
</button>
<button
onClick={() => {
setPage((s) => ({ page: Number(s.page) - 1 }));
}}
style={{ margin: '0 8px' }}
>
-
</button>
<button
onClick={() => {
setPage({ page: undefined });
}}
>
reset
</button>
</span>
</div>
<br />
<div>
pageSize: {pageSize.pageSize}
<span style={{ paddingLeft: 8 }}>
<button
onClick={() => {
setPageSize((s) => ({ pageSize: Number(s.pageSize) + 1 }));
}}
>
+
</button>
<button
onClick={() => {
setPageSize((s) => ({ pageSize: Number(s.pageSize) - 1 }));
}}
style={{ margin: '0 8px' }}
>
-
</button>
<button
onClick={() => {
setPageSize({ pageSize: undefined });
}}
>
reset
</button>
</span>
</div>
<div>
<button
onClick={async () => {
await setPageSize({ pageSize: undefined });
await setPage({ page: undefined });
}}
>
reset all
</button>
</div>
</>
);
};
19 changes: 11 additions & 8 deletions packages/use-url-state/use-url-state.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ npm i @ahooksjs/use-url-state -S
>
> 2\. Installed @ahooksjs/use-url-state

## Usage

```js
Expand All @@ -44,6 +43,10 @@ React Router V6:https://codesandbox.io/s/autumn-shape-odrt9?file=/App.tsx

<code src="./demo/demo2.tsx" hideActions='["CSB"]' />

### Multi-state management (split)

<code src="./demo/demo4.tsx" hideActions='["CSB"]' />

### Custom query-string options

<code src="./demo/demo3.tsx" hideActions='["CSB"]' />
Expand All @@ -57,21 +60,21 @@ const [state, setState] = useUrlState(initialState, options);
### Params

| Property | Description | Type | Default |
|--------------|--------------------------------|----------------|---------|
| ------------ | ------------------------------ | -------------- | ------- |
| initialState | InitialState, same as useState | `S \| () => S` | - |
| options | Url config | `Options` | - |

### Options

| Property | Description | Type | Default |
|------------------|--------------------------------------------------------------------------------------------------------------|-----------------------|----------|
| navigateMode | Type of history navigate mode | `'push' \| 'replace'` | `'push'` |
| parseOptions | [parse](https://github.com/sindresorhus/query-string#parsestring-options) options of `query-string` | `ParseOptions` | - |
| stringifyOptions | [stringify](https://github.com/sindresorhus/query-string#stringifyobject-options) options of `query-string` | `StringifyOptions` | - |
| Property | Description | Type | Default |
| ---------------- | ----------------------------------------------------------------------------------------------------------- | --------------------- | -------- |
| navigateMode | Type of history navigate mode | `'push' \| 'replace'` | `'push'` |
| parseOptions | [parse](https://github.com/sindresorhus/query-string#parsestring-options) options of `query-string` | `ParseOptions` | - |
| stringifyOptions | [stringify](https://github.com/sindresorhus/query-string#stringifyobject-options) options of `query-string` | `StringifyOptions` | - |

### Result

| Property | Description | Type |
|----------|----------------------------------------------|---------------------------------------------------|
| -------- | -------------------------------------------- | ------------------------------------------------- |
| state | Url query object | `object` |
| setState | Same as useState, but state should be object | `(state: S) => void \| (() => ((state: S) => S))` |
10 changes: 7 additions & 3 deletions packages/use-url-state/use-url-state.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ React Router V6:https://codesandbox.io/s/autumn-shape-odrt9?file=/App.tsx

<code src="./demo/demo2.tsx" hideActions='["CSB"]' />

### 多状态管理(拆分)

<code src="./demo/demo4.tsx" hideActions='["CSB"]' />

### 自定义 query-string 配置

<code src="./demo/demo3.tsx" hideActions='["CSB"]' />
Expand All @@ -56,21 +60,21 @@ const [state, setState] = useUrlState(initialState, options);
### Params

| 参数 | 说明 | 类型 | 默认值 |
|--------------|----------|----------------|--------|
| ------------ | -------- | -------------- | ------ |
| initialState | 初始状态 | `S \| () => S` | - |
| options | url 配置 | `Options` | - |

### Options

| 参数 | 说明 | 类型 | 默认值 |
|------------------|---------------------------------------------------------------------------------------------------------|-----------------------|----------|
| ---------------- | ------------------------------------------------------------------------------------------------------- | --------------------- | -------- |
| navigateMode | 状态变更时切换 history 的方式 | `'push' \| 'replace'` | `'push'` |
| parseOptions | `query-string` [parse](https://github.com/sindresorhus/query-string#parsestring-options) 的配置 | `ParseOptions` | - |
| stringifyOptions | `query-string` [stringify](https://github.com/sindresorhus/query-string#stringifyobject-options) 的配置 | `StringifyOptions` | - |

### Result

| 参数 | 说明 | 类型 |
|----------|-----------------------------------------|---------------------------------------------------|
| -------- | --------------------------------------- | ------------------------------------------------- |
| state | url query 对象 | `object` |
| setState | 用法同 useState,但 state 需要是 object | `(state: S) => void \| (() => ((state: S) => S))` |

0 comments on commit 0fb401e

Please sign in to comment.