reactjs - 尝试有条件地禁用链接时的 TS2339
问题描述
在一个表中,我有一个链接,我想有条件地变灰。以下代码在更新之前运行良好:
<Link to="#"
onClick={e => { e.preventDefault(); }}
disabled={this.props.finalisingStatus} >
Finalize up to here
</Link>
在标题中,我import { Link } from "react-router-dom"
像往常一样做了。在组件的接口中,我定义了一个属性finalisingStatus: Boolean;
。
更新到 Typescript(版本 4.41.2)后,我收到 TS2339 错误:
类型“IntrinsicAttributes & IntrinsicClassAttributes> & Readonly> & Readonly<{ children?: ReactNode; 上不存在属性‘disabled’ }>'
我该如何解决?
PS:React 组件中的 TypeScript TS2339 错误:类型 'IntrinsicAttributes...' 上不存在属性 'xyz'有点相关,但在我的情况下不是问题。
解决方案
I would recommend you to render Link
conditionally, as disabled
might not be a valid prop of Link
. This way, you will not need to worry about overwriting the click event that is triggered by Link
.
If it is disabled, you render your span
element with the disabled styles.
Here is a simple example:
render () {
const { isDisabled } = this.props;
if(isDisabled){
return <span className='disabled'>{this.props.linktext}</span>;
} else {
return <Link to={this.props.to}>{this.props.linktext}</Link>;
}
}
推荐阅读
- log4j - 在 Anypoint studio 7 中编码时无法配置 log4j 行为
- kotlin - 实现使用类的属性
- python - 使用 scikit 绘制具有不同概率的多类分类器的 ROC 曲线
- java - 使用 java 权限问题运行 docker
- excel - 如何从excel中提取第5个和第6个最右边的字符
- javascript - image.src = canvas.toDataURL("image/png"); 保存问题
- r - 勾勒geom_bar的整个bar
- javascript - 为所有输入类型创建通用文本框
- macos - MAC OS 上的 QT 窗口无法正确刷新
- java - IntelliJ 调试器仅触发一次断点,直到重新启动应用程序