-
-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from juliencrn/feature/add-lifecycle-hooks
feat(hooks): Create lifecycles hooks
- Loading branch information
Showing
25 changed files
with
1,809 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,2 @@ | ||
export { default as useEffectOnce } from './useEffectOnce' | ||
export * from './useEffectOnce' |
This file contains 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,16 @@ | ||
import { renderHook } from '@testing-library/react-hooks/native' | ||
|
||
import useEffectOnce from './useEffectOnce' | ||
|
||
describe('use effect once()', () => { | ||
test('should be triggered only once', () => { | ||
const effect = jest.fn() | ||
const { rerender } = renderHook(() => useEffectOnce(effect)) | ||
|
||
expect(effect).toHaveBeenCalledTimes(1) | ||
|
||
rerender() | ||
|
||
expect(effect).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
This file contains 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,8 @@ | ||
import { EffectCallback, useEffect } from 'react' | ||
|
||
function useEffectOnce(effect: EffectCallback) { | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
useEffect(effect, []) | ||
} | ||
|
||
export default useEffectOnce |
This file contains 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,2 @@ | ||
export { default as useIsFirstRender } from './useIsFirstRender' | ||
export * from './useIsFirstRender' |
This file contains 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,15 @@ | ||
import { renderHook } from '@testing-library/react-hooks/native' | ||
|
||
import useIsFirstRender from './useIsFirstRender' | ||
|
||
describe('use is first render()', () => { | ||
test('should return true at the first render, next false', () => { | ||
const { result, rerender } = renderHook(() => useIsFirstRender()) | ||
|
||
expect(result.current).toBe(true) | ||
|
||
rerender() | ||
|
||
expect(result.current).toBe(false) | ||
}) | ||
}) |
This file contains 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,15 @@ | ||
import { useRef } from 'react' | ||
|
||
function useIsFirstRender(): boolean { | ||
const isFirst = useRef(true) | ||
|
||
if (isFirst.current) { | ||
isFirst.current = false | ||
|
||
return true | ||
} | ||
|
||
return isFirst.current | ||
} | ||
|
||
export default useIsFirstRender |
This file contains 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,2 @@ | ||
export { default as useUpdateEffect } from './useUpdateEffect' | ||
export * from './useUpdateEffect' |
This file contains 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,16 @@ | ||
import { renderHook } from '@testing-library/react-hooks/native' | ||
|
||
import useUpdateEffect from './useUpdateEffect' | ||
|
||
describe('use update effect()', () => { | ||
test('the callback function should have been called on update', () => { | ||
const effect = jest.fn() | ||
const { rerender } = renderHook(() => useUpdateEffect(effect)) | ||
|
||
expect(effect).not.toHaveBeenCalled() | ||
|
||
rerender() | ||
|
||
expect(effect).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
This file contains 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,16 @@ | ||
import { DependencyList, EffectCallback, useEffect } from 'react' | ||
|
||
import { useIsFirstRender } from '../useIsFirstRender' | ||
|
||
function useUpdateEffect(effect: EffectCallback, deps?: DependencyList) { | ||
const isFirst = useIsFirstRender() | ||
|
||
useEffect(() => { | ||
if (!isFirst) { | ||
return effect() | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, deps) | ||
} | ||
|
||
export default useUpdateEffect |
Oops, something went wrong.