我正在学习 React
+ Typescript
,但我遇到了一个奇怪的问题。本质上,我已经定义了一个 FunctionComponent
:
const AddLogModal: React.FC<{ addLog: Function }> = ({ addLog }) => {
const [message, setMessage] = useState('');
const [attention, setAttention] = useState(false);
const [tech, setTech] = useState('');
我正在尝试将 attention
变量分配给 checkbox
值,所以我做了:
<input
type="checkbox"
className="filled-in"
checked={attention}
value={attention}
onChange={e => setAttention(!attention)}
/>
到目前为止我得到这个错误:
Type 'boolean' is not assignable to type 'string | number | readonly string[] | undefined'.
我做错了什么?
最佳答案
复选框的状态是通过 checked
属性设置的 only - 它也不需要 value
。删除这部分:
value={attention}
https://stackoverflow.com/questions/64378450/
相关文章:
c++ - 仅当使用 unordered_map 而不是 vector 时,将 const 作为此参
haskell - 在 Haskell 的 do 上下文中应用构造函数
python - Windows 上的 Gcloud 命令(使用 git bash)正在记录错误 :
javascript - 在 React Native 中安排本地通知不起作用
azure-devops - Azure Devops - Terraform 任务失败并出现错误
java - 如何使用 void set 方法在静态常量帮助程序类中创建对象?
react-native - React native 每次打开页面时如何执行函数