-
Notifications
You must be signed in to change notification settings - Fork 2
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 #2 from mblink/useLayoutEffect
Added useStableLayoutEffect
- Loading branch information
Showing
8 changed files
with
58 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { useStable, useStableE, useStableO } from './state'; | ||
export { useStableEffect } from './effect'; | ||
export { useStableLayoutEffect } from './layoutEffect'; | ||
export { useStableCallback } from './callback'; | ||
export { useStableMemo } from './memo'; |
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,11 @@ | ||
import * as Eq from 'fp-ts/Eq'; | ||
import { EffectCallback, useLayoutEffect } from 'react'; | ||
import { useEqMemoize } from './useEqMemoize'; | ||
|
||
export const useStableLayoutEffect = <A extends ReadonlyArray<unknown>>( | ||
callback: EffectCallback, | ||
dependencies: A, | ||
eq: Eq.Eq<A> | ||
): ReturnType<typeof useLayoutEffect> => { | ||
return useLayoutEffect(callback, useEqMemoize(dependencies, eq)); | ||
}; |
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,27 @@ | ||
import * as O from 'fp-ts/Option'; | ||
import { nEq, o1a, o1b, o2 } from './state'; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useStableLayoutEffect } from '../src/index'; | ||
|
||
describe('useStableLayoutEffect', () => { | ||
test('should not be called if the values are the same', () => { | ||
let o = o1a; | ||
const { rerender } = renderHook(() => { | ||
useStableLayoutEffect(() => { o = o2; }, [o], nEq); | ||
}); | ||
o = o1b; | ||
rerender(); | ||
expect(o).toStrictEqual(o1a); | ||
}); | ||
|
||
test('should be called if the values are different', () => { | ||
const o99 = O.some(99); | ||
let o = o1a; | ||
const { rerender } = renderHook(() => { | ||
useStableLayoutEffect(() => { o = o99; }, [o], nEq); | ||
}); | ||
o = o2; | ||
rerender(); | ||
expect(o).toStrictEqual(o99); | ||
}); | ||
}); |
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,13 @@ | ||
import * as E from 'fp-ts/Either'; | ||
import * as Eq from 'fp-ts/Eq'; | ||
import * as O from 'fp-ts/Option'; | ||
|
||
export const o1a = O.some(1); | ||
export const o1b = O.some(1); | ||
export const o2 = O.some(2); | ||
|
||
export const e1a = E.right(1); | ||
export const e1b = E.right(1); | ||
export const e2 = E.right(2); | ||
|
||
export const nEq = Eq.getTupleEq(O.getEq(Eq.eqNumber)); |