简体   繁体   中英

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' 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")
  }

Try onChange={this.handleChange} instead Your way, you are calling handle change and setting its return value ("void") as the change handler. Under certain circumstances this might be appropriate, but not in yours.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM