Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material UI Tab pass props to component

I'm trying to integrate react-router with Material-UI V1 Tabs, as in this github issue, this stackoverflow post, and the answer they come to errors out for me.

To my understanding, this is how you are supposed to execute this:

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

However I get the error [ts] property 'to' does not exist on type....

I have also tried:

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

But in this case my Tab component does not render at all.

Thoughts?

like image 621
Ethan Avatar asked Dec 23 '22 06:12

Ethan


1 Answers

The source of the problem is that Tab type is declared this way:

declare const Tab: React.ComponentType<TabProps>;

TabProps of course doesn't have to property.

To solve the problem declare a new variable called LinkTab:

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

And use it like below:

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

This should compile just fine.

Full code example with imports:

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")
);
like image 56
IdanDavidi Avatar answered Jan 07 '23 02:01

IdanDavidi