Skip to content

Commit

Permalink
fix: fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
ttys026 committed Oct 10, 2019
2 parents c367fc4 + c87c3f4 commit 5e828a8
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umijs/hooks",
"version": "1.1.0",
"version": "1.1.1",
"description": "react hooks library",
"keywords": [
"umi hooks",
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import useDynamicList from './useDynamicList';
import useEventEmitter from './useEventEmitter';
import { configResponsive, useResponsive } from './useResponsive';
import useSize from './useSize';
import useLocalStorageState from './useLocalStorageState';

export {
useAntdTable,
Expand All @@ -19,7 +20,8 @@ export {
useDynamicList,
useResponsive,
useEventEmitter,
useLocalStorageState,
useSize,
configResponsive,
configRequest,
useSize,
};
14 changes: 4 additions & 10 deletions src/useAPI/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,10 @@ export const configRequest = (method: () => any) => {
const useAPI = <T = any>(opt: IProps<T>) => {
const requestMethod = opt.method || globalMethod || fetch;
return useAsync<T>(
async () =>
new Promise<T>((resolve, reject) => {
requestMethod(opt.url, opt.options)
.then(async res => {
resolve(res.json && typeof res.json === 'function' ? res.json() : res);
})
.catch(e => {
reject(e);
});
}),
async () => {
const res = await requestMethod(opt.url, opt.options);
return res.json && typeof res.json === 'function' ? res.json() : res;
},
[JSON.stringify(opt)],
{
manual: opt.manual,
Expand Down
4 changes: 2 additions & 2 deletions src/useAntdTable/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import useAntdTable from './';
const [gender, setGender] = useState();

const getTableData = ({ current, pageSize }) => {
console.log(current, pageSize);
console.log(current, pageSize, gender);
return fetch(`https://randomuser.me/api?results=55&page=${current}&size=${pageSize}&gender=${gender}`)
.then(res => {
return res.json();
Expand Down Expand Up @@ -82,7 +82,7 @@ import useAntdTable from './';

return (
<>
<Select style={{ width: 180, marginBottom: 24 }} onChange={setGender} placeholder='性别筛选' allowClear>
<Select style={{ width: 180, marginBottom: 24 }} onChange={(g)=>setGender(g)} placeholder='性别筛选' allowClear>
<Option value="male">male</Option>
<Option value="female">female</Option>
</Select>
Expand Down
2 changes: 1 addition & 1 deletion src/useAntdTable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export default function useAntdTable<Result, Item>(

const stateRef = useRef<UseTableInitState>(({} as unknown) as UseTableInitState);
stateRef.current = state;
const { run, loading } = useAsync(fn, [], {
const { run, loading } = useAsync(fn, deps, {
manual: true,
});

Expand Down
13 changes: 7 additions & 6 deletions src/useEventEmitter/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,26 @@ describe('useEventEmitter', () => {
const event$ = useEventEmitter<number>();
const [count, setCount] = useState(0);
event$.useSubscription(val => {
setCount(count + val);
setCount(c => c + val);
});
event$.useSubscription(val => {
setCount(c => c + val + 10);
});
return {
event$,
count,
};
});

it('getKey should work', () => {
it('emit and subscribe should work', () => {
const hook = setUp();
act(() => {
hook.result.current.event$.emit(1);
});
expect(hook.result.current.count).toEqual(1);
expect(hook.result.current.count).toEqual(12);
act(() => {
hook.result.current.event$.emit(2);
});
expect(hook.result.current.count).toEqual(3);
// expect(hook.result.current.getKey(1)).toEqual(1);
// expect(hook.result.current.getKey(2)).toEqual(2);
expect(hook.result.current.count).toEqual(26);
});
});
34 changes: 34 additions & 0 deletions src/useLocalStorageState/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { renderHook, act } from '@testing-library/react-hooks';
import { useState } from 'react';
import useLocalStorageState from '../index';

describe('useLocalStorageState', () => {
it('should be defined', () => {
expect(useLocalStorageState).toBeDefined();
});

const setUp = (): any =>
renderHook(() => {
const [state, setState] = useLocalStorageState('test-key', 'A');
return {
state,
setState,
};
});

it('getKey should work', () => {
const hook = setUp();
expect(hook.result.current.state).toEqual('A');
act(() => {
hook.result.current.setState('B');
});
expect(hook.result.current.state).toEqual('B');
const anotherHook = setUp();
expect(anotherHook.result.current.state).toEqual('B');
act(() => {
anotherHook.result.current.setState('C');
});
expect(anotherHook.result.current.state).toEqual('C');
expect(hook.result.current.state).toEqual('B');
});
});
54 changes: 54 additions & 0 deletions src/useLocalStorageState/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
name: useLocalStorageState
route: /useLocalStorageState
edit: false
sidebar: true
---

import { Playground } from 'docz';
import { useRef } from 'react';
import { Button, Input } from 'antd';
import useLocalStorageState from './index';

# useLocalStorageState

一个可以将状态持久化存储在 localStorage 中的 Hook 。

它的API和 `useState` 非常类似,但是多了一个参数 `key` ,用来指定在 localStorage 中存储时所使用的 `key` 。而它的返回值类型和 `useState` 保持来一致,当调用 `setState` 时,它会自动将新值写入到 localStorage 中。

如果想从 localStorage 中删除这条数据,可以使用 `setState()``setState(undefined)`

## 代码演示

<Playground>
{
()=>{
function Demo() {
const [message, setMessage] = useLocalStorageState('user-message', 'Hello~')
return (
<>
<p>刷新页面后,可以看到输入框中的内容被从 localStorage 中恢复了。</p>
<Input
value={message}
onChange={(e) => {setMessage(e.target.value)}}
placeholder='请输入文字'
style={{width: 200, marginRight: 16}}
/>
<Button onClick={() => {setMessage()}}>重置</Button>
</>
)
}

return <Demo />
}
}
</Playground>

## API

```typescript
function useLocalStorageState<T extends string = string>(
key: string,
defaultValue?: T,
): [T?, (value?: T) => void]
```
18 changes: 18 additions & 0 deletions src/useLocalStorageState/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useState } from 'react';

export default function useLocalStorageState<T extends string = string>(
key: string,
defaultValue?: T,
) {
const [state, setState] = useState(() => (localStorage.getItem(key) as T) || defaultValue);
function updateState(value?: T) {
if (value === undefined) {
localStorage.removeItem(key);
setState(defaultValue);
} else {
localStorage.setItem(key, value as string);
setState(value);
}
}
return [state, updateState] as [typeof state, typeof updateState];
}

0 comments on commit 5e828a8

Please sign in to comment.