javascript - Type Void is Not Assignable to Type Boolean (React/Typescript)
问题描述
I'm getting an error where I want to post a switch boolean value on Change. I'm using React/Typescript... What I want to do here send a post request of the boolean value in the handleChange() function, how would I do that?
The current error i'm getting is Type 'void' is not assignable to type '((event: ChangeEvent<HTMLInputElement>, checked: boolean) => void) | undefined'
interface IState {
application?: Map<{}, {}>;
hasChanged?: boolean;
checkedA?: boolean;
}
<div className={classes.switchContainer}>
<FormGroup row>
<FormControlLabel
control={
<Switch
checked={this.state.checkedA}
onChange={this.handleChange()}
value="checkedA"
>Toggle</Switch>
}label="YES"
/>
<Typography color="secondary" variant="body1" className={classes.toggleQuestions}>Is the Mentor information complete?</Typography>
</FormGroup>
</div>
@autobind
private handleChange() {
console.log("checkedA")
}
解决方案
尝试 onChange={this.handleChange} 以您的方式,您正在调用句柄更改并将其返回值(“void”)设置为更改处理程序。在某些情况下,这可能是合适的,但不适用于您的情况。
推荐阅读
- javascript - 我应该如何使用节点在猫鼬的“组”模型中将新用户添加到用户列表中?
- javascript - 在onclick函数中获取Class的变量
- html - 如何将 FileReader 结果加载到 HTML5 视频中以实现跨浏览器和设备的最佳兼容性
- python - 带有 if 语句的 lambda 函数的语法无效
- c# - 为列表中的每个项目设置权重
- haskell - Haskell - 你如何制作一个包含 getLine 的循环?
- python - 如何使用一个主键将不同的 CSV 文件合并为一个新的 CSV
- html - 在将发送表单的站点中嵌入另一个网页的适当方法是什么?
- python - 用于服务多个 Django 应用程序 docker 容器的 nginx 反向代理配置问题
- javascript - 如何正确创建作为另一个副本的新对象?