简体   繁体   English

typescript 中的 useContext 和 useReducer

[英]useContext and useReducer in typescript

I'm new to typescript and I was implementing react useContext and useReducer.我是typescript的新手,我正在实施 react useContext 和 useReducer。 I was following a tutorial but im getting an error of Parameter 'action' implicitly has an 'any' type.我正在关注一个教程,但我得到一个Parameter 'action' implicitly has an 'any' type. In my reducer function.在我的减速机 function 中。

reducer function减速机function

function reducer(state, action) { // error Parameter 'action' implicitly has an 'any' type.
     return { count: state.count + 1 }
}

CountProvider计数提供者

   const [state, dispatch] = useReducer(reducer, {
    count: 0
   })

You have to specify the type of action as any or do it by generic form.您必须将操作类型指定为 any 或以通用形式执行。 In case you want to structure action you could create an interface.如果您想构建动作,您可以创建一个界面。

function reducer(state, action: { [key: string]: string|number }) { // error Parameter 'action' implicitly has an 'any' type.
    return { count: state.count + 1 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM