Getting values from material-ui Button in React

若如初见. 提交于 2019-12-24 06:35:53

问题


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?


回答1:


Use currentTarget instead of target

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


来源:https://stackoverflow.com/questions/50414059/getting-values-from-material-ui-button-in-react

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!