我有一组选项卡,之后要添加按钮(添加新选项卡)。我刚刚在Tabs组件的子代中添加了一个按钮,它可以按照我的期望进行渲染和工作,但是在开发人员控制台中有很多警告。
<AppBar position="static">
<Tabs
value={this.props.selectedTab}
onChange={this.handleTabSelect}
textColor="secondary"
>
{this.props.ListOfStuff.map(el => {
return (
<Tab
className={classes.tabButton}
value={el.ClientId}
label={el.Label}
id={el.ClientId}
aria-controls={"tabPanel-" + el.ClientId}
key={"tab-" + el.ClientId}
/>
);
})}
<Button
className={classes.addButton}
onClick={this.addNewTab}
>
<AddIcon color="secondary" className={classes.addIcon} />
NEW TAB
</Button>
</Tabs>
</AppBar>
警告如下:
-警告:收到false
的非布尔属性indicator
。
-警告:React无法识别DOM元素上的textColor
属性。
-警告:ForwardRef(InputBase)包含类型为time的输入,其中包含value和defaultValue属性。
关于如何以相同的方式呈现它,但摆脱所有这些警告的任何建议吗?
谢谢
答案 0 :(得分:1)
Tabs
组件clones its child elements(假定为Tab
元素)可以传递其他属性(例如与“ selected”标签相关的属性)。
警告是由这些附加属性传递到Button
组件引起的。您可以通过将Button
包装在忽略Tabs
传递的其他属性的组件中来解决这些警告,例如:
const ButtonInTabs = ({ className, onClick, children }) => {
return <Button className={className} onClick={onClick} children={children} />;
};
完整的示例:
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 Button from "@material-ui/core/Button";
import AddIcon from "@material-ui/icons/Add";
function TabPanel(props) {
const { children, value, index, ...other } = props;
return (
<Typography
component="div"
role="tabpanel"
hidden={value !== index}
id={`simple-tabpanel-${index}`}
aria-labelledby={`simple-tab-${index}`}
{...other}
>
<Box p={3}>{children}</Box>
</Typography>
);
}
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
},
addButton: {
color: "white"
}
}));
const ButtonInTabs = ({ className, onClick, children }) => {
return <Button className={className} onClick={onClick} children={children} />;
};
export default function SimpleTabs() {
const classes = useStyles();
const [value, setValue] = React.useState(0);
const [showThirdTab, setShowThirdTab] = React.useState(false);
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)} />
{showThirdTab && <Tab label="Item Three" {...a11yProps(2)} />}
<ButtonInTabs
onClick={() => setShowThirdTab(true)}
className={classes.addButton}
>
<AddIcon color="secondary" />
New Tab
</ButtonInTabs>
</Tabs>
</AppBar>
<TabPanel value={value} index={0}>
Item One
</TabPanel>
<TabPanel value={value} index={1}>
Item Two
</TabPanel>
<TabPanel value={value} index={2}>
Item Three
</TabPanel>
</div>
);
}