Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting values from material-ui Button in React

I have this function

handleChangeButton = (e) => {
    alert(e.target.value)
    this.props.setFieldValue('degreeLevel', e.target.value);
  }

and in my component render, I have

<div className="twelve columns">
            <p>Degree Level</p>
            <Button
              variant="raised"
              label="Default"
              onClick = { this.handleChangeButton }
              value="Doctorate"
            >
              Doctorate
            </Button>

            <Button variant="raised" label="Default">
              Masters
            </Button>

            <Button variant="raised" label="Default">
              Undergraduate
            </Button>
          </div>

So, what I want to do is, when I click the Doctorate button, it should this.props.setFieldValue to degreeLevel which is one of the fields in my Formik form. When I click the button, the alert gives me undefined which means it's not reading the value Doctorate.

How can I make e.target.value read the value of the button?

like image 477
Dawn17 Avatar asked Nov 28 '22 03:11

Dawn17


1 Answers

Use currentTarget instead of target

handleChangeButton = (e) => {
    alert(e.currentTarget.value)
    this.props.setFieldValue('degreeLevel', e.currentTarget.value);
}
like image 183
Piyush Bhati Avatar answered Dec 03 '22 19:12

Piyush Bhati