Skip to content

Commit

Permalink
Merge pull request #2 from mblink/useLayoutEffect
Browse files Browse the repository at this point in the history
Added useStableLayoutEffect
  • Loading branch information
jleider authored Nov 23, 2021
2 parents 2d08a6f + ccca7af commit 8695347
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/index.ts
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';
11 changes: 11 additions & 0 deletions src/layoutEffect.ts
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));
};
7 changes: 1 addition & 6 deletions test/callback.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import * as Eq from 'fp-ts/Eq';
import * as O from 'fp-ts/Option';
import { nEq, o1a, o1b, o2 } from './state';
import { renderHook } from '@testing-library/react-hooks';
import { useStableCallback } from '../src/index';

const o1a = O.some(1);
const o1b = O.some(1);
const o2 = O.some(2);
const nEq = Eq.getTupleEq(O.getEq(Eq.eqNumber));

describe('useStableCallback', () => {
test('should not return a new function if the values are the same', () => {
let o = o1a;
Expand Down
10 changes: 3 additions & 7 deletions test/effect.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import * as Eq from 'fp-ts/Eq';
import * as O from 'fp-ts/Option';
import { nEq, o1a, o1b, o2 } from './state';
import { renderHook } from '@testing-library/react-hooks';
import { useStableEffect } from '../src/index';

const o1a = O.some(1);
const o1b = O.some(1);
const o2 = O.some(2);

describe('useStableEffect', () => {
test('should not be called if the values are the same', () => {
let o = o1a;
const { rerender } = renderHook(() => {
useStableEffect(() => { o = o2; }, [o], Eq.getTupleEq(O.getEq(Eq.eqNumber)));
useStableEffect(() => { o = o2; }, [o], nEq);
});
o = o1b;
rerender();
Expand All @@ -22,7 +18,7 @@ describe('useStableEffect', () => {
const o99 = O.some(99);
let o = o1a;
const { rerender } = renderHook(() => {
useStableEffect(() => { o = o99; }, [o], Eq.getTupleEq(O.getEq(Eq.eqNumber)));
useStableEffect(() => { o = o99; }, [o], nEq);
});
o = o2;
rerender();
Expand Down
27 changes: 27 additions & 0 deletions test/layoutEffect.test.ts
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);
});
});
4 changes: 1 addition & 3 deletions test/memo.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import * as Eq from 'fp-ts/Eq';
import * as O from 'fp-ts/Option';
import { o1a, o1b, o2 } from './state';
import { renderHook } from '@testing-library/react-hooks';
import { useStableMemo } from '../src/index';

const o1a = O.some(1);
const o1b = O.some(1);
const o2 = O.some(2);
const nEq = Eq.getTupleEq(O.getEq(Eq.eqNumber));

describe('useStableMemo', () => {
Expand Down
9 changes: 1 addition & 8 deletions test/state.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import * as E from 'fp-ts/Either';
import * as Eq from 'fp-ts/Eq';
import * as O from 'fp-ts/Option';
import { act, renderHook } from '@testing-library/react-hooks';
import { e1a, e1b, e2, o1a, o1b, o2 } from './state';
import { useStable, useStableE, useStableO } from '../src/index';
import { Dispatch } from 'react';

const o1a = O.some(1);
const o1b = O.some(1);
const o2 = O.some(2);
const e1a = E.right(1);
const e1b = E.right(1);
const e2 = E.right(2);

type Test<A = any> = {describe: string, fn: (a: A) => [A, Dispatch<A>], val1a: A, val1b: A, val2: A};
const tests: Test[] = [
{
Expand Down
13 changes: 13 additions & 0 deletions test/state.ts
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));

0 comments on commit 8695347

Please sign in to comment.