【问题标题】:How to Add route links to material ui tabs in react typescript如何在反应打字稿中将路由链接添加到材料 ui 选项卡
【发布时间】:2020-11-30 13:35:18
【问题描述】:

我在反应中使用 ts。如何将路由链接添加到我的代码中的选项卡

import React from 'react';
import { makeStyles, Theme } 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';

interface TabPanelProps {
  children?: React.ReactNode;
  index: any;
  value: any;
}

function TabPanel(props: TabPanelProps) {
  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>
  );
}

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

const useStyles = makeStyles((theme: 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: React.ChangeEvent<{}>, newValue: number) => {
    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}>
        Item One
      </TabPanel>
      <TabPanel value={value} index={1}>
        Item Two
      </TabPanel>
      <TabPanel value={value} index={2}>
        Item Three
      </TabPanel>
    </div>
  );
}

我想制作 /home/item1、/home/item2 等链接。

如何在 react 的 typescript 中添加这个。在选项卡上导航应更新浏览器地址栏上的 url。 或者如果用户直接点击 /home/item2,它应该默认打开该选项卡

【问题讨论】:

    标签: javascript reactjs typescript react-router material-ui


    【解决方案1】:

    您可以使用component 属性将Tab 设置为Link,如下所示:

    import { Link } from 'react-router-dom'
    
    ...
    
    <Tab
      ...
      component={Link}
      to="/home/item1"
    />
    

    注意,它还通过任何其他非 Tab 属性传递给 component,因此您可以指定 Linkto 属性或您需要设置的任何其他属性。

    要从路线中设置选定的选项卡,请从该位置找出适当的索引(您可以从 useLocation 获取此索引)并使用该索引而不是硬编码为 0 来初始化 value 状态:

    const location = useLocation()
    const selectedIndex = figureOutSelectedIndex(location)
    
    const [value, setValue] = React.useState(selectedIndex);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-20
      • 1970-01-01
      • 1970-01-01
      • 2021-02-24
      • 2019-04-15
      • 2018-03-24
      • 2017-11-09
      • 2021-10-18
      相关资源
      最近更新 更多