道具类型失败:道具“ children”在“ InputAdornment”中标记为必需,但其值为“ undefined”

时间:2019-02-21 09:22:14

标签: javascript reactjs

运行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>,
    }}

有人可以解释什么地方出了问题,我该如何避免此警告?

1 个答案:

答案 0 :(得分:1)

在编写<InputAdornment position="start"></InputAdornment>时,您不会将任何子代传递给InputAdornment(例如,如果我想在InputAdornment的<InputAdornment position="start"><MyComponent /></InputAdornment>中传递MyComponent,则可以编写children。 同时,道具children在InputAdornment的required中被指定为PropTypes。 删除警告有两种方法:

  1. 如果您不希望始终将子项prop传递给InputAdornement,则可以从其required中删除PropTypes指定符。
  2. 如果您确实希望始终将子级prop传递给InputAdornement,或者它不是您的组件,则应如上例中那样将children传递给它。然后将满足PropType's的要求。