简体   繁体   English

mdl-js-ripple-effect 导致 event.target.value 在 React 组件中未定义

[英]mdl-js-ripple-effect causes event.target.value to become undefined in a React component

When using vanilla mdl ( <script src="material.js" > ) or using react-mdl , if I add the mdl-js-ripple-effect mdl class (or the ripple attribute with react-mdl ) to a button, the event.target.value of the button element becomes undefined (the event handler is modifying state in react).当使用 vanilla mdl ( <script src="material.js" > ) 或使用react-mdl ,如果我将mdl-js-ripple-effect mdl 类(或带有react-mdl的ripple 属性)添加到按钮,按钮元素的event.target.value变得未定义(事件处理程序正在修改响应中的状态)。 Without the ripple effect, it works just fine.没有涟漪效应,它工作得很好。 I can't find a solution other than not using the ripple effect;除了使用涟漪效应,我找不到其他解决方案; but that makes the button very boring.但这使按钮非常无聊。 There seem to be issues with using react with mdl, but I thought someone might know more... (I'm using create-react-app )使用反应与 mdl 似乎存在问题,但我认为有人可能知道更多......(我正在使用create-react-app

The click handler:点击处理程序:

  handleButtonClick(event){
    event.preventDefault();
    this.setState({input: this.state.input + event.target.value});
  }

The "Key" React component using react-mdl :使用react-mdl的“关键”React 组件:

function Key (props) {
    return(
      <Button raised colored ripple
        value={props.value}
        onClick={props.handleButtonClick}>
        {props.value}
      </Button>
    );
}

The same issue occurs if I use vanilla mdl with a button element:如果我将 vanilla mdl 与button元素一起使用,则会出现同样的问题:

function Key (props) {
    return(
        <button className="mdl-button mdl-js-button mdl-button--raised 
            mdl-js-ripple-effect mdl-button--colored"
            value={props.value}
            onClick={props.handleButtonClick}>
          {props.value}
       </button>
    );
}

If I remove the ripple, then event.target.value is what it should be ( value={props.value} ) when the button is clicked.如果我删除涟漪,那么当单击按钮时event.target.value应该是它( value={props.value} )。 But with the ripple, it is undefined.随着涟漪,它是不确定的。

Anyone experienced this, or have an idea of why this is happening, or a work-around?任何人都经历过这种情况,或者知道为什么会发生这种情况,或者有什么解决方法?

The mdl-js-ripple-effect class replaces the button element with a styled span element, removing it's value attribute. mdl-js-ripple-effect类用样式化的span元素替换button元素,删除它的value属性。

You can access the value attribute via this.props.value instead.您可以通过this.props.value访问value属性。

Here's a jsFiddle showing the values of event.target.value vs this.props.value .这是一个jsFiddle,显示了event.target.valuethis.props.value的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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