material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

Can't seem to integrate the Tabs Component and React Router to change url.

Open davidfufu opened this issue 9 years ago • 13 comments

Just as a preface, I have checked stackoverlow's material UI section.

I'm having trouble integrating React routing's Link Tag with Material UI Tabs.

I've tried putting the Link in the Tab. The result is that there are no errors, but upon clicking the Tabs, the url does not change.

<Tabs
          value={this.state.value}
          onChange={this.handleChange}
          indicatorColor="primary"
          textColor="primary"
          centered
        >

          <Tab className={classes.tabby}label="Facebook" >
                    <Link to='/Facebook'>Facebook</Link>
          </Tab>
          <Tab className={classes.tabby}label="LinkedIn" >
                    <Link to='/LinkedIn'>LinkedIn</Link>
          </Tab>
          <Tab className={classes.tabby}label="Twitter" >
                    <Link to='/Twitter'>Twitter</Link>
          </Tab>
</Tabs>

I've also tried passing the Link in using the children property. This doesn't change the url either, nor does cause any errors.

          <Tab className={classes.tabby}label="Facebook" children={<Link to="/Facebook" />}></Tab>
          <Tab className={classes.tabby}label="LinkedIn" children={<Link to="/LinkedIn" />}/>
          <Tab className={classes.tabby}label="Twitter" children={<Link to="/Twitter" />}/>

Finally wrapping Tab in Link as given below breaks the tab bar component and returns an error of:

<Link to='/Facebook'>Facebook
              <Tab className={classes.tabby}label="Facebook" />
</Link>
warning.js:36 Warning: Unknown props `fullWidth`, `textColor` on <a> tag. Remove these props from the element. For details, see https://fb.me/react-unknown-prop
    in a (created by Link)
    in Link (created by PlatformTabs)
"material-ui": "^1.0.0-beta.10",
"material-ui-icons": "^1.0.0-beta.5",
"react": "^15.6.1",
"react-dom": "^15.6.1",
Chrome: Version 61.0.3163.79 (Official Build) (64-bit)
"react-router-dom": "^4.2.2",

davidfufu avatar Sep 21 '17 18:09 davidfufu

Do something like:

<Tab label="Facebook" component={Link} to="/facebook" />

oliviertassinari avatar Sep 21 '17 18:09 oliviertassinari

Current Tab and Link Code:

//PLATFORMTABS.JS
<Tabs
          value={this.state.value}
          onChange={this.handleChange}
          indicatorColor="primary"
          textColor="primary"
          centered
        >

          <Tab className={classes.tabby} label="Facebook" component={Link} to="/Facebook" />
          
          <Tab className={classes.tabby} label="LinkedIn" component={Link} to="/LinkedIn"/>
          <Tab className={classes.tabby} label="Twitter" component={Link} to="/Twitter"/>
        </Tabs>


My route code looks like this.

return(
			<div style={styles.background}>
				<div style={styles.dayContainer}>
					<TopBar/>
					<PlatformTabs/>
					<Switch>
						<Route path='/Facebook' component={FacebookPosts}/>
						<Route path='/LinkedIn' component={LinkedInPosts}/>
						<Route path='/Twitter' component={TwitterPosts}/>
					</Switch>
					<SimpleLineChart/>
				</div>
			</div>

		)

But, once I load the page, this error shows up.

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object. Check the render method of `Route`.
    in Route (created by PostVisual)
    in Switch (created by PostVisual)
    in div (created by PostVisual)
    in div (created by PostVisual)
    in PostVisual (created by App)
    in div (created by App)
    in App
    in Router (created by BrowserRouter)
    in BrowserRouter
    in AppContainer
    in div

My components that the routes are supposed to render are not there either and if I click any tab, this error is given.

ReactCompositeComponent.js:744 Uncaught TypeError: Cannot read property '_currentElement' of null
    at ReactCompositeComponentWrapper._updateRenderedComponent (ReactCompositeComponent.js:744)
    at ReactCompositeComponentWrapper._performComponentUpdate (ReactCompositeComponent.js:723)
    at ReactCompositeComponentWrapper.updateComponent (ReactCompositeComponent.js:644)
    at ReactCompositeComponentWrapper.receiveComponent (ReactCompositeComponent.js:546)
    at Object.receiveComponent (ReactReconciler.js:124)
    at ReactCompositeComponentWrapper._updateRenderedComponent (ReactCompositeComponent.js:753)
    at ReactCompositeComponentWrapper._performComponentUpdate (ReactCompositeComponent.js:723)
    at ReactCompositeComponentWrapper.updateComponent (ReactCompositeComponent.js:644)
    at ReactCompositeComponentWrapper.receiveComponent (ReactCompositeComponent.js:546)
    at Object.receiveComponent (ReactReconciler.js:124)

davidfufu avatar Sep 21 '17 19:09 davidfufu

Nevermind, I changed how I was importing and it's working. Thanks!

davidfufu avatar Sep 21 '17 20:09 davidfufu

@davidfufu perhaps you could elaborate how you fixed this?

ksaitor avatar Nov 20 '17 08:11 ksaitor

I believe I switched to "export default Component". Before, I was using multiple defaults from one file and destructuring them when importing like "import { Component1, Component2}".

davidfufu avatar Nov 21 '17 18:11 davidfufu

<Tab label="Facebook" component={Link} to="/facebook" />, from material UI doesn't have property to

evalcraciun avatar Sep 03 '18 11:09 evalcraciun

@evalcraciun Link has a to property.

oliviertassinari avatar Sep 03 '18 14:09 oliviertassinari

I got this error message

Type 'typeof Link' is not assignable to type '"object" | "a" | "abbr" | "address" | "area" | "article" | "aside" | "audio" | "b" | "base" | "bdi" | "bdo" | "big" | "blockquote" | "body" | "br" | "button" | "canvas" | "caption" | ... 96 more ... | undefined'.   Type 'typeof Link' is not assignable to type 'ComponentClass<ButtonBaseProps, any>'.     Types of parameters 'props' and 'props' are incompatible.       Property 'to' is missing in type 'ButtonBaseProps' but required in type 'Readonly'.

129emma avatar Jan 13 '19 20:01 129emma

@129emma #14170

oliviertassinari avatar Jan 13 '19 20:01 oliviertassinari

This method makes it so that when a tab is pressed, the link hover text stays visible at the bottom of the page until you click elsewhere or select a different tab.

EduardOrdukhanov avatar Feb 25 '19 18:02 EduardOrdukhanov

What about Next.js Link component? It doesn't have a to property but a href one.

How could I add a link to a tab using the Next.js Link component?

Edit: @oliviertassinari , the solution you propose (i.e. <Tab label="Facebook" component={Link} to="/facebook" />) doesn't seem to work anymore. The doc mentions a "component prop" (here that doesn't exist anymore).

lcswillems avatar Oct 18 '19 11:10 lcswillems

This solution should still be valid: https://material-ui.com/guides/composition/#routing-libraries

oliviertassinari avatar Oct 18 '19 12:10 oliviertassinari

Thank you for the answer!

lcswillems avatar Oct 18 '19 17:10 lcswillems