Material UI Tab将props传递给组件

时间:2018-06-14 17:40:36

标签: reactjs typescript react-router material-ui

我正在尝试将react-router与Material-UI V1选项卡集成在一起,就像在this github issuethis stackoverflow post中一样,以及他们为我出错的答案。

根据我的理解,这就是你应该如何执行这个:

import Tab from '@material-ui/core/Tab';
import Link from 'react-router-dom';
/* other code */
<Tab component={Link} to="/" value={'/'} key={'/'} label={'/'}/>

但是我收到错误[ts] property 'to' does not exist on type...

我也尝试过:

<Tab component={() => <Link to='/' />} value={'/'} key={'/'}] label={'/'}/>

但在这种情况下,我的Tab组件根本不会呈现。

思想?

2 个答案:

答案 0 :(得分:4)

问题的根源是这样声明Tab类型:

declare const Tab: React.ComponentType<TabProps>;

TabProps当然没有to属性。

要解决此问题,请声明一个名为LinkTab的新变量:

const LinkTab: React.ComponentType<TabProps & LinkProps> = Tab as React.ComponentType<TabProps & LinkProps>;

并按如下所示使用它:

<LinkTab icon={<ListIcon />} component={Link} to="/monitor" />

这应该可以编译。

带有导入的完整代码示例:

import * as React from "react";
import * as ReactDOM from "react-dom";
import { MonitorContainer } from "Monitor/MonitorContainer";
import { HashRouter as Router, Route, Redirect, Switch, Link, LinkProps } from 'react-router-dom';
import { RemoteContainer } from "Remote/RemoteContainer";
import AppBar from '@material-ui/core/AppBar';
import Tabs from '@material-ui/core/Tabs';
import { default as Tab, TabProps } from '@material-ui/core/Tab';
import ListIcon from '@material-ui/icons/ListAlt';
import RemoteControlIcon from '@material-ui/icons/SettingsRemote';
import './reset.css';
import './index.scss';
import '../assets/fonts/icons/flaticon.css';

const LinkTab: React.ComponentType<TabProps & LinkProps> = Tab as React.ComponentType<TabProps & LinkProps>;

export interface AppState {
    value: number;
}

export class App extends React.Component<{}, AppState> {
    constructor(props: Readonly<{}>) {
        super(props);

        this.state = {
            value: 0,
        };
    }

    handleChange = (event: any, value: any) => {
        this.setState({ value });
    };

    render() {
        const { value } = this.state;

        return (
        <div>
            <AppBar position="static">
                <Tabs value={value} onChange={this.handleChange} scrollable scrollButtons="off">
                    <LinkTab icon={<ListIcon />} component={Link} to="/monitor" />
                    <LinkTab icon={<RemoteControlIcon />} component={Link} to="/remote" />
                </Tabs>
            </AppBar>
        </div>
        )
    }
};

ReactDOM.render(
    <Router>
        <div>
            <App />
            <Switch>
                <Route exact path="/monitor" component={ MonitorContainer } />
                <Route exact path="/remote" component={ RemoteContainer } />
                <Redirect from="/" to="/monitor" />
            </Switch>
        </div>
    </Router>,
    document.getElementById("root")
);

答案 1 :(得分:0)

我遇到了同样的问题,最终我的解决方案是将<Tab>替换为<Link>

<AppBar>
  <Link to="/links" className={styles.tabButton}>Links</Link>
  <Link to="/docs" className={styles.tabButton}>Docs</Link>
  <Link to="/videos" className={styles.tabButton}>Videos</Link>
</AppBar>
<Switch>
  <Route path="/links" component={MediaLinks} />
  <Route path="/docs" component={MediaDocs} />
  <Route path="/videos" component={MediaVideos} />
</Switch>