Skip to content
Open
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
9 changes: 9 additions & 0 deletions src/ref.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Eq } from 'fp-ts/Eq';
import { useRef as useRef_ } from 'react';

export const useStableRef = <A>(a: A, eq: Eq<A>): A => {
const ref = useRef_<A>(a);
if (!eq.equals(ref.current, a))
ref.current = a;
return ref.current;
};
30 changes: 21 additions & 9 deletions src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,32 @@ import * as E from 'fp-ts/Either';
import * as Eq from 'fp-ts/Eq';
import * as O from 'fp-ts/Option';
import { Dispatch, SetStateAction, useReducer } from 'react';
import type { Endomorphism } from 'fp-ts/Endomorphism';

const isSetStateFn = <A>(s: SetStateAction<A>): s is (a: A) => A => typeof s === 'function';
import Either = E.Either;
import Option = O.Option;

export const useStable = <A>(initState: A, eq: Eq.Eq<A>): [A, Dispatch<SetStateAction<A>>] =>
type Eq<A> = Eq.Eq<A>; // eslint-disable-line @typescript-eslint/no-redeclare
type StateTuple<A> = [A, Dispatch<SetStateAction<A>>];

/** @internal */
const isSetter = <A>(s: SetStateAction<A>): s is Endomorphism<A> => typeof s === 'function';

export const useStable = <A>(initState: A, eq: Eq<A>): StateTuple<A> =>
useReducer(
(s1: A, s2: SetStateAction<A>) => {
const _s2 = isSetStateFn(s2) ? s2(s1) : s2;
return eq.equals(s1, _s2) ? s1 : _s2;
(prev: A, ss: SetStateAction<A>) => {
const next = isSetter(ss) ? ss(prev) : ss;
return eq.equals(prev, next) ? prev : next;
},
initState
);

export const useStableO = <A>(initState: O.Option<A>): [O.Option<A>, Dispatch<SetStateAction<O.Option<A>>>] =>
useStable(initState, O.getEq(Eq.eqStrict));
export const useStableO = <A>(initialState: Option<A>, eq: Eq<A> = Eq.eqStrict): StateTuple<Option<A>> =>
useStable(initialState, O.getEq(eq));

export const useStableE = <E, A>(initState: E.Either<E, A>): [E.Either<E, A>, Dispatch<SetStateAction<E.Either<E, A>>>] =>
useStable(initState, E.getEq(Eq.eqStrict, Eq.eqStrict));
export function useStableE<E, A>(initialState: Either<E, A>): StateTuple<Either<E, A>>;
export function useStableE<E, A>(initialState: Either<E, A>, leftEq: Eq<E>, rightEq: Eq<A>): StateTuple<Either<E, A>>;
export function useStableE<E, A>(initialState: Either<E, A>, leftEq?: Eq<E>, rightEq?: Eq<A>): StateTuple<Either<E, A>> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Its possible with this signature that a user could provide a left but not a right Eq or vice versa if undefined is passed as a left. In that case this function does not behave as you would expect since its explicitly checking if both exist.

if (leftEq && rightEq) return useStable(initialState, E.getEq(leftEq, rightEq));
else return useStable(initialState, E.getEq(Eq.eqStrict, Eq.eqStrict));
}