【问题标题】:Failed prop type: The prop `children` is marked as required in `InputAdornment`, but its value is `undefined`失败的道具类型:道具`children`在`InputAdornment`中标记为必填,但其值为`undefined`
【发布时间】:2019-07-15 03:37:45
【问题描述】:

我在运行 React.js 前端时收到此警告:

index.js:1446 Warning: Failed prop type: The prop `children` is marked as required in `InputAdornment`, but its value is `undefined`.
    in InputAdornment (created by WithStyles(InputAdornment))
    in WithStyles(InputAdornment) (at TopControls.js:101)
    in div (created by InputBase)
    in InputBase (created by Context.Consumer)
    in WithFormControlContext(InputBase) (created by WithStyles(WithFormControlContext(InputBase)))
    in WithStyles(WithFormControlContext(InputBase)) (created by Input)
    in Input (created by WithStyles(Input))
    in WithStyles(Input) (created by TextField)
    in div (created by FormControl)
    in FormControl (created by WithStyles(FormControl))
    in WithStyles(FormControl) (created by TextField)
    in TextField (at TopControls.js:91)
    in div (created by Grid)
    in Grid (created by WithStyles(Grid))
    in WithStyles(Grid) (at TopControls.js:90)
    in div (created by Grid)
    in Grid (created by WithStyles(Grid))
    in WithStyles(Grid) (at TopControls.js:89)
    in div (at TopControls.js:22)
    in TopControls (at BatchFlights.js:147)
    in BatchFlights (created by WithStyles(BatchFlights))
    in WithStyles(BatchFlights) (at App.js:23)

TopControls.js 中的第 10-102 行包含以下代码:

    InputProps={{
       startAdornment: <InputAdornment position="start"></InputAdornment>,
    }}

有人可以解释什么是错的,我该如何避免这个警告?

【问题讨论】:

  • InputAdornment 发生了什么?该组件需要一个您没有传递给它的道具
  • @Amir-Mousavi: InputAdornment 是什么意思?我是 React.js 的新手。的确,也许我可以完全删除它
  • :)) 我不知道InputAdornment是什么意思这是你在你的应用程序中使用的组件。

标签: javascript reactjs


【解决方案1】:

当您编写 &lt;InputAdornment position="start"&gt;&lt;/InputAdornment&gt; 时,您不会将任何子项传递给 InputAdornment(例如,如果我想在 InputAdornment 的 children 中传递 MyComponent,我可以编写 &lt;InputAdornment position="start"&gt;&lt;MyComponent /&gt;&lt;/InputAdornment&gt;)。 同时,在InputAdornment的PropTypes中将propschildren指定为required。 有两种方法可以消除警告:

  1. 如果您不想始终将 children 属性传递给 InputAdornement,您可以从其 PropTypes 中删除 required 说明符。
  2. 如果您确实希望始终将 children 属性传递给 InputAdornement,或者它不是您的组件,您应该像上面的示例一样将 children 传递给它。那么PropType's 的要求就会满足。

【讨论】:

    猜你喜欢
    • 2019-05-21
    • 2020-01-14
    • 2018-03-23
    • 2018-01-24
    • 2022-01-05
    • 2018-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多