From 8bff8821b4e8bb20e1bedb01c07a3ba8f22dfe16 Mon Sep 17 00:00:00 2001 From: Valerii Sidorenko Date: Tue, 2 Jul 2024 13:14:38 +0200 Subject: [PATCH] feat(useControlledState): support update callback with additional params (#1688) --- .../useControlledState/useControlledState.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/hooks/useControlledState/useControlledState.ts b/src/hooks/useControlledState/useControlledState.ts index 60a8555b7a..67fbdb8283 100644 --- a/src/hooks/useControlledState/useControlledState.ts +++ b/src/hooks/useControlledState/useControlledState.ts @@ -1,19 +1,19 @@ import React from 'react'; -export function useControlledState( +export function useControlledState( value: Exclude, defaultValue: Exclude | undefined, - onChange?: (v: C, ...args: any[]) => void, -): [T, (value: C) => void]; -export function useControlledState( + onChange?: (v: C, ...args: Args) => void, +): [T, (value: C, ...args: Args) => void]; +export function useControlledState( value: Exclude | undefined, defaultValue: Exclude, - onChange?: (v: C, ...args: any[]) => void, -): [T, (value: C) => void]; -export function useControlledState( + onChange?: (v: C, ...args: Args) => void, +): [T, (value: C, ...args: Args) => void]; +export function useControlledState( value: T, defaultValue: T, - onUpdate?: (value: C, ...args: any[]) => void, + onUpdate?: (value: C, ...args: Args) => void, ) { const [innerValue, setInnerValue] = React.useState(value ?? defaultValue); @@ -37,7 +37,7 @@ export function useControlledState( // that we call `onUpdate` inside the callback function and onUpdate // in a controlling component frequently calls setState itself, // therefore we call `setState` while we're rendering a different component. - (newValue: C, ...args: any[]) => { + (newValue: C, ...args: Args) => { if (!Object.is(currentValue, newValue)) { onUpdate?.(newValue, ...args); }