reactjs - React & Redux tslint 警告
问题描述
当我使用 react 和 redux 时。该connect
方法导致以下 tslint 警告/错误(我也不是 100% 确定该声明)。组件有效,但查看 tslint 错误让我很生气。
类型定义是这个interface IPrivateMenuItem extends StateProps, IMenuItem, IOwnProps {}
输入'{孩子:字符串;图标:“用户”;到:字符串;}' 不可分配给类型 'IntrinsicAttributes & Pick<IPrivateMenuItem, "hasAnyAuthorities" | “身份证” | “到” | “图标”> & IOwnProps'。'IntrinsicAttributes & Pick<IPrivateMenuItem, "hasAnyAuthorities" | 类型上不存在属性 'children' “身份证” | “到” | “图标”> & IOwnProps'.ts(2322)
我尝试了像const PrivateMenuItem: React.FC<IPrivateMenuItem>
. 但是警告没有机会消失。除非我删除连接语句。由于 redux 的要求,这是不可能的。
然后,我将功能组件的 prop 类型切换为如下定义的IPrivateMenuItem。然后问题就没有了。我的问题是这是最佳做法还是有更简单的做法?
interface IPrivateMenuItem extends StateProps, IMenuItem, React.PropsWithChildren<IOwnProps> {}
没有导入的完整功能组件代码是
interface IOwnProps {
hasAnyAuthorities?: string[];
}
interface IPrivateMenuItem extends StateProps, IMenuItem, React.PropsWithChildren<IOwnProps> {}
const PrivateMenuItem = ({ isAuthorized, id, to, icon, children }: IPrivateMenuItem) => {
return isAuthorized ? (
<MenuItem id={id} to={to} icon={icon}>
{children}
</MenuItem>
) : (
<></>
);
};
const mapStateToProps = ({ authentication: { account } }: IRootState, { hasAnyAuthorities = [] }: IOwnProps) => ({
isAuthorized: hasAnyAuthority(account.authorities, hasAnyAuthorities),
});
type StateProps = ReturnType<typeof mapStateToProps>;
export default connect(mapStateToProps)(PrivateMenuItem);
解决方案
没有看到整个组件有点困难,但如果它有帮助
连接的完整类型如下
connect<TheMapStateToPropsType, TheDispatchToPropsType, TheComponentPropsType, TheStorePropsType>
你可以像这样输入你的组件:(我是用一个类来做的,但是你得到了一个功能组件的精神)
interface OwnProps { }
interface StateProps { }
interface DispatchProps { }
type Props = OwnProps & StateProps & DispatchProps;
export class MyComponent extends Component<Props> { }
const mapStateToProps = (state): StateProps => ({
})
const mapDispatchToProps = (dispatch): DispatchProps => {
return {
}
}
export default connect<StateProps,DispatchProps,OwnProps,StorePropsIfYouWant>(mapStateToProps, mapDispatchToProps)(MyComponent)
推荐阅读
- javascript - 将用户信息存储在 Web 服务器根目录的单个文件中 JavaScript/HTML/CSS
- python - 使用 scipy 创建大脑信号的小波变换
- typescript - tsconfig.json 设置错误 - 有关系吗?
- reactjs - 图表上的菜单下拉菜单正在偏移
- mongodb - Mongo Restore 不知道如何处理文件错误
- tfs - TFS 源代码控制的回滚得到错误 EntityFramework.PowerShell.Utility.dll' 被拒绝
- python - 拉取新代码时出现 Django cron 作业错误
- r - 如何在 dplyr 的 group_by 上使用 if/ifelse
- vuejs2 - 如何在 Vuejs 中进行多表数据汇总
- codeigniter-3 - 动态网站的站点地图 - codeigniter