【发布时间】:2019-06-07 12:41:28
【问题描述】:
我收到一个错误,我想在 Change 上发布一个开关布尔值。我正在使用 React/Typescript... 我想在这里做的事情是在 handleChange() 函数中发送一个布尔值的发布请求,我该怎么做?
我得到的当前错误是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")
}
【问题讨论】:
-
只是一个小注解,没有必要使用自动绑定注释(除非它是 Angular...)。为了正确绑定
this,您只需将您的方法定义如下private readonly handleChange = () => {/* do something */};,它将自动在lambda 主体中绑定this。
标签: javascript reactjs typescript ecmascript-2016