首页 > 解决方案 > 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")
  }

标签: javascriptreactjstypescriptecmascript-2016

解决方案


尝试 onChange={this.handleChange} 以您的方式,您正在调用句柄更改并将其返回值(“void”)设置为更改处理程序。在某些情况下,这可能是合适的,但不适用于您的情况。


推荐阅读