Skip to content

Commit 4dac30a

Browse files
committed
Use own useEvent definition instead of using json-immutability-helper's hooks, since we don't need any of the other provided hooks
1 parent e24c2a5 commit 4dac30a

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { useEvent } from './useEvent';
22

3-
type Action<A extends readonly unknown[]> = (...args: A) => void;
4-
53
export const useActionFactory =
64
<Spec>(dispatch: ((spec: Spec) => void) | undefined) =>
7-
<A extends readonly unknown[]>(
5+
<A extends any[]>(
86
action: (...args: A) => Spec,
9-
): Action<A> | undefined => {
7+
): ((...args: A) => void) | undefined => {
108
const fn = useEvent((...args: A) => dispatch?.(action(...args)));
119
return dispatch ? fn : undefined;
1210
};

frontend/src/hooks/useEvent.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
import React from 'react';
2-
import { makeHooks } from 'json-immutability-helper/helpers/hooks';
3-
import { context } from '../api/reducer';
1+
import { useLayoutEffect, useRef, useState } from 'react';
42

5-
export const { useEvent } = makeHooks(context, React);
3+
export const useEvent = <Fn extends (...args: any) => any>(fn: Fn) => {
4+
const latest = useRef(fn);
5+
useLayoutEffect(() => {
6+
latest.current = fn;
7+
}, [fn]);
8+
const [stable] = useState(
9+
() =>
10+
(...args: Parameters<Fn>): ReturnType<Fn> =>
11+
latest.current.apply(undefined, args),
12+
);
13+
return stable;
14+
};

0 commit comments

Comments
 (0)