reactjs - 尝试将布尔和接口传递给组件状态时,React/Typescript 中的属性缺少类型?
问题描述
我是 React 和 Typescript 的新手,我找不到将它们传递到 React.Componet 状态的正确方法。我的代码仍然按预期工作,但我想知道我做错了什么!
class App extends React.Component<{}, { isLoading: boolean, thisCoffeeShop: ICoffeeShop }> {
constructor(props: Readonly<{}>) {
super(props);
this.state = {
isLoading: true
}
}
//....
}
}
我假设它的打字稿把我扔了
"Property 'thisCoffeeShop' is missing in type '{ isLoading: true; }' but required in type 'Readonly<{ isLoading: boolean; thisCoffeeShop: ICoffeeShop; }>'."
解决方案
你的状态丢失thisCoffeeShop
了,你说会在其中。
this.state = {
isLoading: true
thisCoffeeShop: {...} // no idea what structure ICoffeeShop is
}
如果您不希望它被要求启动,您可以将该属性设为可选。
React.Component<{}, { isLoading: boolean, thisCoffeeShop?: ICoffeeShop }>
推荐阅读
- ansible - vagrant provision 在 stouts.celery 运行 ansible playbook 失败
- python - 删除明确定义了块的开头和结尾的句子块
- javascript - 如何在 Node.js 中的主进程和子进程之间传递流输入/输出
- android - 如何建立表之间的关系
- c++ - SDL_image.h 不工作,#include "sdl.h" :没有这样的文件或目录
- nuxt.js - 当我运行 nuxt porject 时,出现应用程序崩溃错误
- pyspark - 将结构数组分解为 pyspark 中的列
- reactjs - 无法使用来自 axios 请求的 request.form 在烧瓶 api 中获取请求正文
- html - 将多个 div 组合并对齐为一个形状
- c++ - 脚本运行后引用 lua 表/对象