Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: useUpdateEffect #103

Merged
merged 6 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"pub:doc": "now --prod"
},
"dependencies": {
"react-use": "^10.6.2",
"resize-observer-polyfill": "^1.5.1"
},
"peerDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import useVirtualList from './useVirtualList';
import { configResponsive, useResponsive } from './useResponsive';
import useSize from './useSize';
import useLocalStorageState from './useLocalStorageState';
import useUpdateEffect from './useUpdateEffect';

export {
useAntdTable,
Expand All @@ -26,4 +27,5 @@ export {
useSize,
configResponsive,
configRequest,
useUpdateEffect,
};
2 changes: 1 addition & 1 deletion src/useAntdTable/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { WrappedFormUtils } from 'antd/lib/form/Form';
import { PaginationConfig } from 'antd/lib/pagination';
import { DependencyList, useEffect, useReducer, useRef, useCallback } from 'react';
import { useUpdateEffect } from 'react-use';
import useUpdateEffect from '../useUpdateEffect';
import useAsync from '../useAsync';

interface UseAntdTableFormUtils extends WrappedFormUtils {
Expand Down
6 changes: 3 additions & 3 deletions src/useAsync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,12 @@ export default function useAsync<Result = any>(
}, []);

useEffect(
(...args: any[]) => {
() => {
if (options.pollingInterval) {
intervalAsync();
} else if (!options.manual) {
// 直接值行
run(...(args || []));
// 直接执行
run();
}

return () => {
Expand Down
2 changes: 1 addition & 1 deletion src/useControlledValue/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMemo, useState, useCallback } from 'react';
import { useUpdateEffect } from 'react-use';
import useUpdateEffect from '../useUpdateEffect';

export interface Options {
defaultValuePropName?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/useLoadMore/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DependencyList, RefObject, useEffect, useState, useCallback, useRef } from 'react';
import { useUpdateEffect } from 'react-use';
import useUpdateEffect from '../useUpdateEffect';
import useAsync from '../useAsync';

export interface ReturnValue<Item> {
Expand Down
2 changes: 1 addition & 1 deletion src/useSearch/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DependencyList, useState, useMemo, useRef, useCallback } from 'react';
import { useUpdateEffect } from 'react-use';
import useUpdateEffect from '../useUpdateEffect';

import useAsync from '../useAsync';

Expand Down
2 changes: 1 addition & 1 deletion src/useSize/__test__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { renderHook, act } from '@testing-library/react-hooks';
import useSize from '../index';

// test about Resize Observer see https://github.com/que-etc/resize-observer-polyfill/tree/master/tests
describe('useSearch', () => {
describe('useSize', () => {
it('should be defined', () => {
expect(useSize).toBeDefined();
});
Expand Down
31 changes: 31 additions & 0 deletions src/useUpdateEffect/__test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { renderHook, act } from '@testing-library/react-hooks';
import useUpdateEffect from '../index';

describe('useUpdateEffect', () => {
it('should be defined', () => {
expect(useUpdateEffect).toBeDefined();
});
it('test on mounted', async () => {
let mountedState = 1;
const hook = renderHook(() =>
useUpdateEffect(() => {
mountedState = 2;
}),
);
expect(mountedState).toEqual(1);
hook.rerender();
expect(mountedState).toEqual(2);
});
it('test on optional', () => {
let mountedState = 1;
const hook = renderHook(() =>
useUpdateEffect(() => {
mountedState = 3;
}, [mountedState]),
);
expect(mountedState).toEqual(1);
mountedState = 2;
hook.rerender();
expect(mountedState).toEqual(3);
});
});
71 changes: 71 additions & 0 deletions src/useUpdateEffect/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
name: useUpdateEffect
route: /useUpdateEffect
menu: '基础 hooks'
edit: false
sidebar: true
---

import { Playground } from 'docz';
import useUpdateEffect from './index';

# useUpdateEffect

一个作用于组件 didMount 阶段以后的 useEffect hook。

## 代码演示

> 使用上和 useEffect 一模一样,只是生命周期放在了组件 didMount 之后。

<Playground>
{
() => {
function Demo() {
const [count, setCount] = React.useState(0);

React.useEffect(() => {
const time = setTimeout(() => {
setCount(count => count + 1)
}, 1000)

return () => {
clearTimeout(time)
}
})

useUpdateEffect(() => {
// you can look console count 1 and beyond
console.log(`count: ${count}`)

return () => {
// do something
}
}) // you can include deps array if necessary

return (
<div>
<p>请打开控制台查看:</p>
Count: {count}
</div>
)
}
return <Demo />
}
}
</Playground>

## API

```js
useUpdateEffect(
effect: () => void,
deps?: deps,
)
```

### 参数

| 参数 | 说明 | 类型 | 默认值 |
|---------|----------------------------------------------|------------------------|--------|
| effect | 可执行函数 | Function | - |
| deps | 可选项,传入依赖变化的对象 | array | array \| undefined | - |
15 changes: 15 additions & 0 deletions src/useUpdateEffect/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useEffect, useRef } from 'react';

const useUpdateEffect: typeof useEffect = (effect, deps) => {
const isMounted = useRef(false);

useEffect(() => {
if (!isMounted.current) {
isMounted.current = true;
} else {
return effect();
}
}, deps);
};

export default useUpdateEffect;