将useReducer函数转换为钩子-React Typescript

时间:2020-10-14 15:45:14

标签: reactjs typescript react-hooks

我有一个使用useReducer的状态更新程序。我想使其成为一个钩子(强类型)。请指教。

interface IState {
  loading: boolean;
  name: string;
}

const initialState: IState = {
  loading: false,
  name: 'Arun'
}

const [state, setState] = useReducer(
  (state: IState, newState: Partial < IState > ) => ({
    ...state,
    ...newState,
  }),
  initialState
);

请建议将此函数包装到强类型的挂钩中。

谢谢。

0 个答案:

没有答案