Search code examples
javascriptreactjstypescriptecmascript-2016

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

Solution

  • 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.