关于TypeScript在ReactHooks里Redux中的使用(一)

时间:2021-01-22 10:05:01   收藏:0   阅读:168

记录一个在React redux中使用Ts的例子:

项目地址:https://github.com/TYoul/react-redux-ts-count

注意点:由于在组件中直接使用useSelector会出现TS无法判断state的类型(具体可以看Count.tsx文件),所以在hooks.tsx文件中,从react-redux导出useSelector并进行如下操作:

`
import { useSelector as useReduxSelector, TypedUseSelectorHook } from ‘react-redux‘;
import { RootState } from ‘./index‘;

export const useSelector: TypedUseSelectorHook = useReduxSelector;
`

在之后的组件中要导入store的数据都要使用这个useSelector。

这个RootState,类型反编译。

export type RootState = ReturnType<typeof store.getState>;

原文:https://www.cnblogs.com/chenZhouXu/p/14311280.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!