【问题标题】:Alternative to Textfield onChange that behaves like HTML onChange?替代 Textfield onChange 的行为类似于 HTML onChange?
【发布时间】:2019-07-09 05:38:39
【问题描述】:

我有一个嵌套在其他组件中的 Textfield 组件,并将函数指针作为道具向下传递给它。目前,我有一个函数将 Textfield 的内容传递回使用 onChange 调用的根组件。查看 Textfield 的 API,每当进行更改时都会调用此函数,这与 HTML 中的 onChange 方法不同,后者仅在表单元素失去焦点时才调用。

框架的其余部分是用 React 编写的,并且该组件嵌套在 Grid 中的另一个组件中。

function Description(props) {

  const {updateDescription} = props;

  function changeDescription(event) {
    updateDescription(event.target.value);
  }

  return (
    <>
      <br></br>
      <TextField 
          margin="dense" 
          id="desc-input"
          type="text"
          onChange={changeDescription}
          multiline
          fullWidth
      />
    </>
  );  
}

是否有替代方法在每次更改后不调用该函数,而是在 Textfield 失去焦点时调用该函数?

【问题讨论】:

标签: javascript reactjs material-ui


【解决方案1】:

您可以使用onBlur 事件,当字段失去焦点时调用该事件

<TextField 
          margin="dense" 
          id="desc-input"
          type="text"
          onBlur={changeDescription}
          multiline
          fullWidth
      />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 2020-07-16
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多