File tree Expand file tree Collapse file tree 2 files changed +15
-8
lines changed
Expand file tree Collapse file tree 2 files changed +15
-8
lines changed Original file line number Diff line number Diff line change 11import { useEvent } from './useEvent' ;
22
3- type Action < A extends readonly unknown [ ] > = ( ...args : A ) => void ;
4-
53export 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 } ;
Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments