javascript useReducer中的参数数目无效

2uluyalo  于 2023-03-16  发布在  Java
关注(0)|答案(1)|浏览(166)
export function React.useReducer<(state: PasswordResetFormState, action: PasswordResetAction) => PasswordResetFormState,PasswordResetFormState>reducer: (state: PasswordResetFormState, action: PasswordResetAction) => PasswordResetFormState,     initializerArg: PasswordResetFormState,     initializer: (arg: PasswordResetFormState) => ReducerStateWithoutAction<(state: PasswordResetFormState, action: PasswordResetAction) => PasswordResetFormState>):    [ReducerStateWithoutAction<(state: PasswordResetFormState, action: PasswordResetAction) => PasswordResetFormState>, DispatchWithoutAction]
An alternative to useState.
useReducer is usually preferable to useState when you have complex state logic that involves multiple sub-values. It also lets you optimize performance for components that trigger deep updates because you can pass dispatch down instead of callbacks.
Version:
16.8.0
See also:
https://reactjs.org/docs/hooks-reference.html#usereducer 
  react
`useReducer(reducer: R, initialState: ReducerState , initializer?: undefined): [ReducerState , Dispatch  >]`, `useReducer(reducer: R, initializerArg: ReducerStateWithoutAction , initializer?: undefined): [ReducerStateWithoutAction , DispatchWithoutAction]`, `useReducer(reducer: R, initializerArg: I & ReducerState , initializer: (arg: (I & ReducerState )) => ReducerState ): [ReducerState , Dispatch  >]`, `useReducer(reducer: R, initializerArg: I, initializer: (arg: I) => ReducerState ): [ReducerState , Dispatch  >]`

我在我的usereducer钩子中遇到了上述打字错误。

const [state, dispatch] = useReducer(
    passwordResetReducer,
    initialPasswordResetFormState
  );

参数数目无效,应为3。

export const passwordResetReducer = (
  state: PasswordResetFormState,
  action: PasswordResetAction
): PasswordResetFormState => {
  switch (action.case) {
    default:
      return { ...state, [action.field]: action.value };
  }
};

export const initialPasswordResetFormState: PasswordResetFormState = {
  email: "",
  password1: "",
  password2: "",
  auth: "",
};

export interface PasswordResetFormState {
  email: string;
  password1: string;
  password2: string;
  auth: string;
}

export enum PasswordResetActionCase {
  GENERAL = "GENERAL",
}

export type PasswordResetAction = {
  case: PasswordResetActionCase.GENERAL;
  value: string;
  field: string;
};

我花了很长时间在这上面,google什么也没返回,我完全不知所措。如果有人能注意到代码中有任何明显的需要修改的地方,我将非常感谢你的帮助。我有多个表单是以同样的方式设置的,这个错误在每个表单中都很常见。我第一次注意到这个错误是在同一个代码库中的其他人分享他们的屏幕时。他们没有出现在我的webstorm中。在重新安装后,他们出现在我面前,我花了一整晚试图修复他们,但无济于事。

在usedreducer初始化中,状态也显示为类型never
已解决-运行npm安装--保存@types/react删 debugging 误

whlutmcx

whlutmcx1#

我遇到了这个问题,似乎解决它的方法是将useReducer函数的初始化器参数键入为ReducerState

const [state, dispatch] = useReducer(reducer, initialState as ReducerState<PasswordResetFormState>);

相关问题