Skip to content

Infra/useDidUpdate #1112

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

Merged
merged 10 commits into from
Jan 3, 2021
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: 1 addition & 0 deletions generatedTypes/hooks/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as useToggleValue } from './useToggleValue';
export { default as useDidUpdate } from './useDidUpdate';
5 changes: 5 additions & 0 deletions generatedTypes/hooks/useDidUpdate/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* This hook avoid calling useEffect on the initial value of his dependency array
*/
declare const useDidUpdate: (callback: () => void, dep: [any]) => void;
export default useDidUpdate;
3 changes: 1 addition & 2 deletions generatedTypes/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ export {
PaddingModifiers,
TypographyModifiers,
ColorsModifiers,
BackgroundColorModifier,
useToggleValue
BackgroundColorModifier
} from './commons/new';
export {default as ActionBar, ActionBarProps} from './components/actionBar';
export {default as Avatar, AvatarPropTypes, AvatarProps} from './components/avatar';
Expand Down
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export {default as useToggleValue} from './useToggleValue';
export {default as useDidUpdate} from './useDidUpdate';
69 changes: 69 additions & 0 deletions src/hooks/useDidUpdate/__tests__/useDidUpdate.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {renderHook} from '@testing-library/react-hooks';
import useDidUpdate from '../index';

describe('useDidUpdate hook tests', () => {
it('Expect action to not be called when the dependencies array value hasn\'t change', () => {
const action = jest.fn();
const dependencies = [false, 'false'];
const {rerender} = renderHook(() => useDidUpdate(action, dependencies));

rerender();
rerender();
rerender();
expect(action.mock.calls.length).toBe(0);
});

it('Expect action to be called when dependencies array has change', () => {
const action = jest.fn();
let dependencies = [false, 'YES YES', 1];
const {rerender} = renderHook(() => useDidUpdate(action, dependencies));

dependencies = [false, 'NO NO', 1];
rerender();
expect(action.mock.calls.length).toBe(1);
});

it('Expect action to be called when dependencies array has change', () => {
const action = jest.fn();
let dependencies = [false, 'YES YES', 1];
const {rerender} = renderHook(() => useDidUpdate(action, dependencies));

dependencies = [false, 'NO NO', 1];
rerender();
expect(action.mock.calls.length).toBe(1);
});

it('Expect action to be called when dependencies array has change', () => {
const action = jest.fn();
let dependencies = [false, 'YES YES', 1];
const {rerender} = renderHook(() => useDidUpdate(action, dependencies));

dependencies = [false, 'NO NO', 1];
rerender();
expect(action.mock.calls.length).toBe(1);
});

it('Expect action to be called when object value in dependencies array has change', () => {
const action = jest.fn();
let dependencies = [false, [1, 2], {name: 'AAA', address: {number: 10}}];
const {rerender} = renderHook(() => useDidUpdate(action, dependencies));

dependencies = [false, [1, 2], {name: 'BBB'}];
rerender();
expect(action.mock.calls.length).toBe(1);

dependencies = [false, [1, 2], {name: 'AAA', address: {number: 20}}];
rerender();
expect(action.mock.calls.length).toBe(2);
});

it('Expect action to be called when value in array dependencies array has change', () => {
const action = jest.fn();
let dependencies = [false, [1, [2]], {name: 'AAA'}];
const {rerender} = renderHook(() => useDidUpdate(action, dependencies));

dependencies = [false, [1, [3]], {name: 'AAA', address: {number: 20}}];
rerender();
expect(action.mock.calls.length).toBe(1);
});
});
22 changes: 22 additions & 0 deletions src/hooks/useDidUpdate/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {useEffect, useRef} from 'react';
import _ from 'lodash';

/**
* This hook avoid calling useEffect on the initial value of his dependency array
*/
const useDidUpdate = (callback: () => void, dep: [any]) => {
const isMounted = useRef<boolean>(false);

useEffect(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe im missing something..
shouldn't there be a code that prevents from this effect to trigger on initial mount?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the inner dependancies set to be as the passed prop dependancies, it will actually start as the same value..

if (!isMounted.current) {
return;
}
callback();
}, [dep]);

useEffect(() => {
isMounted.current = true;
}, []);
};

export default useDidUpdate;