0

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")
  }
1
  • Just a minor sidenote, there is no need to use autobinding annotations (unless it's Angular...). In order to properly bind this you can just define your methods as follows private readonly handleChange = () => {/* do something */}; which will automatically bind this in the body of the lambda. Commented Jan 13, 2019 at 10:55

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

got ya that makes perfect sense. how would i then: get the value of said state of the switch input, and send a post request?
dude its literally in the documentation.... handleChange(event) { console.log(event.target.value); } reactjs.org/docs/forms.html
yeah i'm using typescript so it's a little different from the documentation. but thanks, i'll use this as a reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.