Skip to content

Commit

Permalink
fix: 添加setState支持传递回调方法
Browse files Browse the repository at this point in the history
  • Loading branch information
三少 committed Nov 17, 2021
1 parent f9da97f commit ee8f32b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
16 changes: 11 additions & 5 deletions packages/unite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function useEventEnhancement<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
>(
config: TypeUnite.Option<TState, TAll, TProps>,
setState: React.Dispatch<React.SetStateAction<TState>>,
setState: React.Dispatch<React.SetStateAction<TypeUnite.StateOpt<TState>>>,
setError: React.Dispatch<React.SetStateAction<TypeUnite.IError | undefined>>,
context: React.MutableRefObject<any>,
): TypeUnite.EventEnhancementResponse<TAll, TState> {
Expand All @@ -44,12 +44,18 @@ function useEventEnhancement<
context.current.__init = true

const _setState = function (
obj: Partial<TypeUnite.StateOpt<TState>>,
res:
| Partial<TypeUnite.StateOpt<TState>>
| React.SetStateAction<TypeUnite.StateOpt<TState>>,
): void {
if (context.current.__mounted) {
setState((preState) => {
return { ...preState, ...obj }
})
if (toString.call(res) === '[object Object]') {
setState((preState) => {
return { ...preState, ...res }
})
} else {
setState(res as React.SetStateAction<TypeUnite.StateOpt<TState>>)
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/unite/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ declare namespace Unite {
}

interface InstanceMethods<TState extends IAnyObject> {
setState: (state: Partial<StateOpt<TState>>) => void
setState: (
state: Partial<StateOpt<TState>> | React.SetStateAction<StateOpt<TState>>,
) => void
setError: React.Dispatch<React.SetStateAction<IError | undefined>>
}
interface InstanceProperty<
Expand Down

0 comments on commit ee8f32b

Please # to comment.