【问题标题】:TabPanel: Warning: validateDOMNesting(...): <div> cannot appear as a descendant of <p>TabPanel: 警告: validateDOMNesting(...): <div> 不能作为 <p> 的后代出现
【发布时间】:2020-09-01 17:27:50
【问题描述】:

当我使用材料 ui 代码中的选项卡时效果很好,但是当我使用网格和排版自定义 TabPanel 时遇到此警告,我理解 &lt;div&gt; 不能放在 &lt;p&gt; 内的问题,但需要在 TabPanel 内自定义的解决方案.

我需要这样的东西:

<TabPanel value={value} index={0}>
    <Grid>
      <Grid>Item One</Grid>
    </Grid>
  </TabPanel>

这是我尝试的代码示例:

import React from "react";
import PropTypes from "prop-types";
import { makeStyles } from "@material-ui/core/styles";
import AppBar from "@material-ui/core/AppBar";
import Tabs from "@material-ui/core/Tabs";
import Tab from "@material-ui/core/Tab";
import Typography from "@material-ui/core/Typography";
import Box from "@material-ui/core/Box";
import Grid from "@material-ui/core/Grid";

function TabPanel(props) {
  const { children, value, index, ...other } = props;

  return (
    <div
      role="tabpanel"
      hidden={value !== index}
      id={`simple-tabpanel-${index}`}
      aria-labelledby={`simple-tab-${index}`}
      {...other}
    >
      {value === index && (
        <Box p={3}>
          <Typography>{children}</Typography>
        </Box>
      )}
    </div>
  );
}

TabPanel.propTypes = {
  children: PropTypes.node,
  index: PropTypes.any.isRequired,
  value: PropTypes.any.isRequired
};

function a11yProps(index) {
  return {
    id: `simple-tab-${index}`,
    "aria-controls": `simple-tabpanel-${index}`
  };
}

const useStyles = makeStyles((theme) => ({
  root: {
    flexGrow: 1,
    backgroundColor: theme.palette.background.paper
  }
}));

export default function SimpleTabs() {
  const classes = useStyles();
  const [value, setValue] = React.useState(0);

  const handleChange = (event, newValue) => {
    setValue(newValue);
  };

  return (
    <div className={classes.root}>
      <AppBar position="static">
        <Tabs
          value={value}
          onChange={handleChange}
          aria-label="simple tabs example"
        >
          <Tab label="Item One" {...a11yProps(0)} />
          <Tab label="Item Two" {...a11yProps(1)} />
          <Tab label="Item Three" {...a11yProps(2)} />
        </Tabs>
      </AppBar>
      <TabPanel value={value} index={0}>
        <Grid>
          <Grid>Item One</Grid>
        </Grid>
      </TabPanel>
      <TabPanel value={value} index={1}>
        Item Two
      </TabPanel>
      <TabPanel value={value} index={2}>
        Item Three
      </TabPanel>
    </div>
  );
}

在这里我发现了关于这个mui-org/material-ui git issue的相关问题

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    删除你在树上的&lt;Typography&gt; 组件并将它放在孩子里面; Typography 在导致此错误的 &lt;p&gt; 标记内呈现其子级

    【讨论】:

      猜你喜欢
      • 2019-02-15
      • 2019-01-02
      • 2021-10-25
      • 2021-05-30
      • 2019-04-30
      • 2019-09-01
      • 2023-02-11
      • 2018-01-02
      • 2019-09-13
      相关资源
      最近更新 更多