Skip to content

Commit

Permalink
feat: useUrlState (#446)
Browse files Browse the repository at this point in the history
* feat: useUrlState

* feat: rewrite based on RFC

* docs: update useUrlState doc

* docs: update api

* docs: add getter and setter docs

* fix: edge cases

* feat: rewrite useUrlState based on react-router

* perf: typescript definition

* feat: move useUrlState into seperate package

* test: fix test cases

* feat: refactor useUrlState

* test: fix useUrlState test

* doc: optimiz useUrlState doc

* chore: remove initialState delete when set to undefined

Co-authored-by: 砖家 <brickspert.fjl@antfin.com>
  • Loading branch information
ttys026 and brickspert authored Aug 13, 2020
1 parent b9c4e51 commit a91dd7d
Show file tree
Hide file tree
Showing 18 changed files with 495 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ module.exports = {
parserOptions: {
...fabric.default.parserOptions,
project: './packages/**/tsconfig.json',
}
},
};
2 changes: 1 addition & 1 deletion .umirc.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default {
links: [{ rel: 'manifest', href: '/asset-manifest.json' }],
hash: true,
resolve: {
includes: ['docs', 'packages/hooks/src', 'packages/use-request'],
includes: ['docs', 'packages/hooks/src', 'packages/use-request', 'packages/use-url-state'],
},
links: [
{
Expand Down
23 changes: 11 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ React Hooks Library.

## 📚 Document

* [English](https://ahooks.js.org/)
* [中文](https://ahooks.js.org/zh-CN/)
- [English](https://ahooks.js.org/)
- [中文](https://ahooks.js.org/zh-CN/)

## ✨ Features

* Easy to learn and use.
* Contains a wealth of advanced Hooks that are refined from the app.
* Contains a wealth of basic Hooks.
* Written in TypeScript with predictable static types.
- Easy to learn and use.
- Contains a wealth of advanced Hooks that are refined from the app.
- Contains a wealth of basic Hooks.
- Written in TypeScript with predictable static types.

## 📦 Install

Expand Down Expand Up @@ -79,6 +79,7 @@ $ cd hooks
$ npm run init
$ npm start
```

Open your browser and visit http://127.0.0.1:8001 , see more at Development.

We welcome all contributions, please read our [CONTRIBUTING.MD](https://github.com/alibaba/hooks/blob/master/CONTRIBUTING.MD) first, let's build a better hooks library together.
Expand All @@ -91,9 +92,7 @@ We welcome all contributions, please read our [CONTRIBUTING.MD](https://github.c

[MIT](https://github.com/alibaba/hooks/blob/master/LICENSE)


[1]: https://www.npmjs.com/package/ahooks
[2]: https://npmjs.org/package/ahooks

[image-1]: https://img.shields.io/npm/v/ahooks.svg?style=flat
[image-2]: https://img.shields.io/npm/dm/ahooks.svg?style=flat
[1]: https://www.npmjs.com/package/ahooks
[2]: https://npmjs.org/package/ahooks
[image-1]: https://img.shields.io/npm/v/ahooks.svg?style=flat
[image-2]: https://img.shields.io/npm/dm/ahooks.svg?style=flat
24 changes: 12 additions & 12 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ React Hooks Library.

## 📚 文档

* [English](https://ahooks.js.org/)
* [中文](https://ahooks.js.org/zh-CN/)
- [English](https://ahooks.js.org/)
- [中文](https://ahooks.js.org/zh-CN/)

## ✨ 特性

* 易学易用
* 包含大量提炼自应用的高级 Hooks。
* 包含丰富的基础 Hooks。
* 使用 TypeScript 构建,提供完整的类型定义文件。
- 易学易用
- 包含大量提炼自应用的高级 Hooks。
- 包含丰富的基础 Hooks。
- 使用 TypeScript 构建,提供完整的类型定义文件。

## 📦 安装

```
npm i ahooks --save
```

## 🔨使用
## 🔨 使用

```
import { useRequest } from 'ahooks';
Expand Down Expand Up @@ -79,6 +79,7 @@ $ cd hooks
$ npm run init
$ npm start
```

打开浏览器访问 http://127.0.0.1:8001

我们欢迎所有人参与共建,请参考[CONTRIBUTING.MD](https://github.com/alibaba/hooks/blob/master/CONTRIBUTING.MD)
Expand All @@ -91,8 +92,7 @@ $ npm start

[MIT](https://github.com/alibaba/hooks/blob/master/LICENSE)

[1]: https://www.npmjs.com/package/ahooks
[2]: https://npmjs.org/package/ahooks

[image-1]: https://img.shields.io/npm/v/ahooks.svg?style=flat
[image-2]: https://img.shields.io/npm/dm/ahooks.svg?style=flat
[1]: https://www.npmjs.com/package/ahooks
[2]: https://npmjs.org/package/ahooks
[image-1]: https://img.shields.io/npm/v/ahooks.svg?style=flat
[image-2]: https://img.shields.io/npm/dm/ahooks.svg?style=flat
1 change: 0 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ gulp.task('declaration', function () {
return tsProject.src().pipe(tsProject()).pipe(gulp.dest('es/')).pipe(gulp.dest('lib/'));
});


gulp.task('copyReadme', async function () {
await gulp.src('../../README.md').pipe(gulp.dest('../../packages/hooks'));
});
Expand Down
2 changes: 1 addition & 1 deletion packages/use-request/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ npm install @ahooksjs/use-request
## Example

```javascript
import { useRequest } from '@ahooksjs/use-request';
import useRequest from '@ahooksjs/use-request';

const { data, error, loading, run } = useRequest('/api/user');
```
Expand Down
2 changes: 1 addition & 1 deletion packages/use-request/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function useRequest(service: any, options: any = {}) {
break;
case 'object':
const { url, ...rest } = s;
fn = requestMethod ? requestMethod(s) : fetchProxy(url, rest)
fn = requestMethod ? requestMethod(s) : fetchProxy(url, rest);
break;
}
}
Expand Down
29 changes: 29 additions & 0 deletions packages/use-url-state/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# useUrlState

A hook that stores the state into url query parameters.

## Installing

Inside your React project directory, run the following:

```bash
yarn add @ahooksjs/use-url-state -S
```

Or with npm:

```bash
npm install @ahooksjs/use-url-state -S
```

## Example

```javascript
import useUrlState from '@ahooksjs/use-url-state';

const [state, setState] = useUrlState({ demoCount: '1' });
```

## Documentation

https://ahooks.js.org/hooks/state/use-url-state
30 changes: 30 additions & 0 deletions packages/use-url-state/demo/demo1.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* title: Default usage
* desc: store the state into url query parameter
*
* title.zh-CN: 默认用法
* desc.zh-CN: 将状态同步到 url query 中
*/

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

export default () => {
const [state, setState] = useUrlState({ count: '1' });

return (
<>
<button
style={{ marginRight: 8 }}
type="button"
onClick={() => setState({ count: Number(state.count || 0) + 1 })}
>
add
</button>
<button type="button" onClick={() => setState({ count: undefined })}>
clear
</button>
<div>state: {state?.count}</div>
</>
);
};
79 changes: 79 additions & 0 deletions packages/use-url-state/demo/demo2.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* title: Multi-state management
* desc: useUrlState can manage multiple states at the same time
*
* title.zh-CN: 多状态管理
* desc.zh-CN: useUrlState 可以同时管理多个状态
*/

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

export default () => {
const [state, setState] = useUrlState(
{ page: '1', pageSize: '10' },
{
navigateMode: 'push',
},
);

return (
<>
<div>
page: {state.page}
<span style={{ paddingLeft: 8 }}>
<button
onClick={() => {
setState((s) => ({ page: Number(s.page) + 1 }));
}}
>
+
</button>
<button
onClick={() => {
setState((s) => ({ page: Number(s.page) - 1 }));
}}
style={{ margin: '0 8px' }}
>
-
</button>
<button
onClick={() => {
setState({ page: 1 });
}}
>
reset
</button>
</span>
</div>
<br />
<div>
pageSize: {state.pageSize}
<span style={{ paddingLeft: 8 }}>
<button
onClick={() => {
setState((s) => ({ pageSize: Number(s.pageSize) + 1 }));
}}
>
+
</button>
<button
onClick={() => {
setState((s) => ({ pageSize: Number(s.pageSize) - 1 }));
}}
style={{ margin: '0 8px' }}
>
-
</button>
<button
onClick={() => {
setState({ pageSize: 10 });
}}
>
reset
</button>
</span>
</div>
</>
);
};
3 changes: 3 additions & 0 deletions packages/use-url-state/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const commonConfig = require('../../gulpfile');

exports.default = commonConfig.default;
69 changes: 69 additions & 0 deletions packages/use-url-state/index.en-US.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
title: useUrlState
nav:
title: Hooks
path: /hooks
group:
title: State
path: /state
---

# useUrlState

A hook that stores the state into url query parameters.

## Install

```bash
npm i @ahooksjs/use-url-state -S
```

> This hook relies on useLocation & useHistory from `react-router`, to use this hook, you need to ensure
>
> 1\. Your project is using `react-router` version 5.0 or higher to manage routing
>
> 2\. Installed @ahooksjs/use-url-state

## Usage

```js
import useUrlState from '@ahooksjs/use-url-state';
```

## Examples

### Default usage

<code src="./demo/demo1.tsx" />

### Multi-state management

<code src="./demo/demo2.tsx" />

## API

```typescript
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' | 'replace' |

### Result

| Property | Description | Type |
|----------|------------------------------------------|------------|
| state | url query object | object |
| setState | same as useState, but state should be object | (state: S) => void \| (() => ((state: S) => S)) |
Loading

0 comments on commit a91dd7d

Please sign in to comment.