validateDOMNesting(...): <button> 不能作为 <button> 的后代出现

IT技术 reactjs material-ui
2021-04-18 06:27:36

Material-UI在我的项目中使用,我在控制台中收到警告:

Warning: validateDOMNesting(...): <button> cannot appear as a descendant of <button>

即使没有直系后代,

<form>
  <TextField
    label="Title"
    value={title}
    onChange={this.handleChange("title")}
    className={classes.formControl}
    margin="normal"
  />
  <br />
  <FormControl className={classes.formControl}>
    <InputLabel>Muscles</InputLabel>
    <Select
      value={muscles}
      onChange={this.handleChange("muscles")}
    >
      {categories.map((category, index) => {
        return (
          <MenuItem key="index" value={category}>
            {category}
          </MenuItem>
        );
      })}
    </Select>
  </FormControl>
  <br />
  <TextField
    multiline
    rows={4}
    label="Description"
    value={description}
    onChange={this.handleChange("description")}
    className={classes.formControl}
    margin="normal"
  />
</form>

这是沙盒链接:

https://codesandbox.io/s/react-material-ui-0yqeo

1个回答

默认elementTypeFab组件按钮在您的应用程序中,Fab组件已经嵌套在Button组件中,因此出现错误。Fab组件继承自,ButtonBase因此您应该能够删除顶级Button组件并onClick直接在其上使用

<Fab
  color="secondary"
  aria-label="add"
  size="small"
  onClick={this.handleToggle}
>
  <AddIcon />
</Fab>