【问题标题】:React does not recognize the `InputProps` prop on a DOM elementReact 无法识别 DOM 元素上的 `InputProps` 属性
【发布时间】:2021-12-17 14:01:32
【问题描述】:

警告:React 无法识别 DOM 元素上的 InputProps 属性。如果您有意希望它作为自定义属性出现在 DOM 中,请将其拼写为小写 inputprops。如果您不小心从父组件传递了它,请将其从 DOM 元素中移除。

我不明白我做错了什么。我见过很多类似的问题。但我找不到合适的解决方案。

<TextField
  {...input}
  {...rest}
  name={input.name}
  inputRef={inputRef}
  autoFocus={inputRef.current === document.activeElement}
  disabled={disabled || false}
  multiline={rowCount ? true : false}
  style={{
    width: "100%",
  }}
  onChange={(event) => {
    input.onChange(event.target.value);
  }}
  {...(hesapla
    ? {
        onBlur: (e) => {
          hesapla({ name: input.name, value: input.value });
        },
        onKeyDown: (e) => {
          if (e.key === "Enter") {
            hesapla({ name: input.name, value: input.value });
            e.preventDefault();
          }
        },
      }
    : {})}
  InputProps={{
    classes,
    ...(inputComponent ? { inputComponent: inputComponent } : {}),
    ...(endAdornment ? { endAdornment: endAdornment } : {}),
  }}
  inputProps={{
    style: {
      maxHeight: (rowCount * 16).toString() + "px",
      overflow: "auto",
      ...(rightJustify ? { textAlign: "end" } : {}),
      ...(!readOnly && hesapla
        ? { fontWeight: "bold", borderBottom: "2px solid" }
        : {}),
    },
    readOnly: readOnly ? readOnly : false,
  }}
></TextField>

【问题讨论】:

  • 它在其 Material UI TextField 实例中使用 InputProps。我在自己的代码中使用它,遵循相同的路径。
  • 你有哪个 MUI?
  • "@material-ui/core": "4.11.0", "@material-ui/data-grid": "^4.0.0-alpha.20", "@material-ui /docs": "4.0.0-beta.3", "@material-ui/icons": "4.9.1", "@material-ui/lab": "4.0.0-alpha.56", "@ material-ui/pickers": "3.2.10", "@material-ui/styles": "4.10.0", "@material-ui/system": "4.9.14",

标签: javascript reactjs material-ui


【解决方案1】:

问题在于您的InputProps 对象,根据文档,它使用object,您没有将正确的对象传递给它。

所以,如果你想解决这个问题,我建议你将你的 InputProps 条目创建为一个变量并记录它:

const myInputPropsObject = {
    classes,
    ...(inputComponent ? { inputComponent: inputComponent } : {}),
    ...(endAdornment ? { endAdornment: endAdornment } : {}),
  }

console.log(myInputPropsObject)

注意:空对象的销毁会导致Uncaught SyntaxError: Invalid or unexpected token问题。

检查:

const myObject = {
  name: 'testName',
  ...{},
}

【讨论】:

  • ...(inputComponent ?? { inputComponent: inputComponent }), ...(endAdornment ?? { endAdornment: endAdornment }), @nima 当我像上面那样写代码时,没有错误在控制台中。它工作正常。但我真的不明白这意味着什么,它与我的问题有什么关系。无论如何,我认为我的问题已经解决了。谢谢你的回答。
  • 看看你的实现,例如,如果inputComponentfalse{} 会返回,现在看看注意,类似于@ 987654330@@MüminZEHİR
猜你喜欢
  • 2021-11-19
  • 2018-11-01
  • 2019-01-11
  • 2019-03-25
  • 2019-05-02
  • 2019-10-18
  • 2020-10-05
  • 2021-05-31
  • 2021-12-20
相关资源
最近更新 更多