reactjs - 用条件反应打字稿组件集道具
问题描述
我想在定义变量的值时设置组件的道具。以下是我当前的代码。
import Cropper from 'react-easy-crop'
...
interface State {
...
coverFile: File | null;
...
}
class Test extends React.PureComponent<Props, State> {
state = {
...
coverFile: null,
...
};
...
const file = event.target.files;
self.setState({
coverFile: file[0],
});
<Cropper
image={coverFile?coverFile:undefined}
...
/>
这是错误信息。
No overload matches this call.
Overload 1 of 2, '(props: Readonly<CropperProps>): Cropper', gave the following error.
Type 'null | undefined' is not assignable to type 'string | undefined'.
Type 'null' is not assignable to type 'string | undefined'.
Overload 2 of 2, '(props: CropperProps, context?: any): Cropper', gave the following error.
Type 'null | undefined' is not assignable to type 'string | undefined'.ts(2769)
我怎么解决这个问题?
解决方案
这解释了您的错误消息
输入'空| undefined' 不可分配给类型 'string | 不明确的'。
Cropper
的图像道具需要一个string
或undefined
,但你正在传递null
或undefined
你初始化coverFile
为null
?如果是这样,您可以将其设置为以undefined
消除错误
更新: 如果你不知道 null 来自哪里,你可以简单地这样做:
<Cropper
image={coverFile || undefined}
...
/>
推荐阅读
- python-3.x - How to specify the Nan value in tensorflow it shows many error
- java - 将日期范围匹配计算为 Java
- java - 我有一个制作问答游戏的项目,但是积分变量不断重置
- reactjs - 在 React 和 TypeScript 中使用 typeped init 和 useRef 和 useEffect
- javascript - 服务器端为客户端显示相同的网页
- javascript - MapBox gl js,浏览器控制台安全错误
- javascript - 错误参数不可分配给“AxiosRequestConfig”类型的参数
- javascript - 在 eventListener 中转换后的高度值不正确
- oracle - 在 oracle 中爆炸 clob 数据时缺少行
- android - 应该使用什么框架来开发Android自动化?