【问题标题】:Material-UI React does not recognize the `underlineStyle` prop on a DOM elementMaterial-UI React 无法识别 DOM 元素上的 `underlineStyle` 属性
【发布时间】:2018-09-11 19:37:35
【问题描述】:

我已按照示例代码对材质 UI TextField 元素的下划线颜色进行样式设置。

http://www.material-ui.com/#/components/text-field

但是,当我尝试添加自己的样式时,react 无法识别此属性。

<TextField type="number" id="Commission" label="Commission" underlineStyle={{borderColor : orange500}} fullWidth /> 

我在 chrome 开发者控制台中收到以下错误消息

warning.js:33 Warning: React does not recognize the `underlineStyle` prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase `underlinestyle` instead. If you accidentally passed it from a parent component, remove it from the DOM element.
    in div (created by FormControl)
    in FormControl (created by WithStyles(FormControl))
    in WithStyles(FormControl) (created by TextField)
    in TextField (created by Commissions)
    in div (created by Commissions)
    in div (created by Commissions)
    in Commissions
    in ReactPlaceholder (created by AsyncFunc)
    in AsyncFunc (created by Route)
    in Route (created by App)
    in div (created by App)
    in main (created by App)
    in div (created by App)
    in div (created by App)
    in App (created by Connect(App))
    in Connect(App) (created by Route)
    in Route (created by RestrictedRoute)
    in RestrictedRoute (created by App)
    in div (created by App)
    in IntlProvider (created by App)
    in MuiThemeProvider (created by App)
    in App (created by Connect(App))
    in Connect(App) (created by Route)
    in Route (created by MainApp)
    in Switch (created by MainApp)
    in Router (created by ConnectedRouter)
    in ConnectedRouter (created by MainApp)
    in Provider (created by MainApp)
    in MainApp

npm 查看材质-ui 版本 0.20.0

我已确认 TextField 元素上存在此属性。

我使用的是 Jumbo React 主题,Textfields 的所有下划线颜色默认为紫色。

不知道为什么我的自定义样式没有覆盖TextField 下划线颜色。

【问题讨论】:

  • 你试过underlineStyle={{borderColor : "orange500"}}(带引号)吗?
  • 是的,它仍然不起作用。 orange500 是 material-UI 的一部分,所以我将它们包含在这一行中 import {orange500, blue500} from 'material-ui/colors';
  • @ChrisReeves 应该是 material-ui/styles/colors 而不是 material-ui/colors

标签: javascript reactjs material-ui


【解决方案1】:

在代码中的某处,您将 underlineStyle 属性传递给常规的 DOM element(在本例中为 div)而不是 react 组件

当您使用JSX 渲染常规DOM elements 时,您应该只将有效的DOM attributes 作为props 传递。

这是有效的,因为所有属性都有效DOM attributes

<div className="Bla" id="x" style={{color: 'red'}}>
  ...
</div>

这是无效的,因为myOwnCustomProp 不是有效的DOM attribute

<div myOwnCustomProp='I should not be here'>
  ...
</div>

这不是错误,只是后来 React 版本中引入的警告。 更多信息here

【讨论】:

  • 当我检查页面源时,我看到了这个&lt;div class="MuiFormControl-root-76 MuiFormControl-fullWidth-79" underlinestyle="[object Object]"&gt;&lt;label class="MuiFormLabel-root-86 MuiInputLabel-root-80 MuiInputLabel-formControl-81 MuiInputLabel-animated-84" data-shrink="false" for="Commission"&gt;Commission&lt;/label&gt;&lt;div class="MuiInput-root-92 MuiInput-fullWidth-99 MuiInput-formControl-93 MuiInput-underline-96"&gt;&lt;input type="number" aria-invalid="false" aria-required="false" class="MuiInput-input-100 MuiInput-inputType-104" id="Commission"&gt;&lt;/div&gt;&lt;/div&gt;
  • 恐怕这与 MUI 有关,包括其更新版本的错误?
【解决方案2】:

在 Material-UI v5 中,如果您使用 styled() 函数创建自定义组件,则在使用自定义道具时可能会遇到此问题。要解决这个问题,您需要覆盖 shouldForwardProp 回调并过滤掉不应传递给 DOM 元素的道具:

const Div = styled("div", {
  shouldForwardProp: (props) =>
    props !== "bgColor" && props !== "width" && props !== "height"
})((p) => ({
  backgroundColor: p.bgColor,
  width: p.width + "px",
  height: p.height + "px"
}));

export default function Why() {
  return (
    <>
      <Div width={30} height={30} bgColor="purple" />
      <Div width={60} height={60} bgColor="blue" />
      <Div width={120} height={120} bgColor="green" />
    </>
  );
}

现场演示

【讨论】:

    猜你喜欢
    • 2021-03-12
    • 2021-11-19
    • 2018-11-01
    • 2019-01-11
    • 2019-03-25
    • 2019-05-02
    • 2019-10-18
    • 2020-10-05
    • 2021-05-31
    相关资源
    最近更新 更多