useReducer-类型的参数不能分配给类型'void'.ts的参数。 React&TypeScript

我正在尝试使用useReducer来管理我的应用程序中的状态。我有以下代码:

export interface iState{
  records: IHub[];
}

let globalState: iState = {
  records: []
}

然后在我的组件中,有这个:

  const[state, dispatch ] = useReducer(reducer, globalState);

但是,它在组件的“ globalState”下给了我一条红线,并显示一条消息:

类型“ iState”的参数不能分配给类型“ void”的参数。ts(2345)

What am I doing here? According to this example: https://reactjs.org/docs/hooks-reference.html#usereducer it should work just fine, so is it somethng with TypeScript?

评论
月牙儿
月牙儿

Have your iState (i suggest you rename to IState) extend Reducer

喜欢:

export interface iState extends Reducer<any, any> {
   records: IHub[];
}

Then const[state, dispatch ] = useReducer<IState<any, any>>(reducer: IState, globalState)

点赞
评论