-
Notifications
You must be signed in to change notification settings - Fork 734
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
Infra/useDidUpdate #1112
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
eda4ce8
created useDIdUpdate hook
mendyEdri de9e3e9
added unittests to cover useDidUpdate hook
mendyEdri a2588c5
useDidUpdate export
mendyEdri 9a72830
typescript
mendyEdri 3cc666f
remove unused
mendyEdri 55345b8
remove unused
mendyEdri 2dad747
Merge branch 'infra/useDidUpdate' of github.com:wix/react-native-ui-l…
mendyEdri 4f33ac0
naming
mendyEdri 9c8c976
Avoid re run isEqual again by using useMemo + match dependencies type
mendyEdri 3cf2d30
PR Comment - logic is based on another useEffect check
mendyEdri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(() => { | ||
if (!isMounted.current) { | ||
return; | ||
} | ||
callback(); | ||
}, [dep]); | ||
|
||
useEffect(() => { | ||
isMounted.current = true; | ||
}, []); | ||
}; | ||
|
||
export default useDidUpdate; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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..