In my project I have a TabComponent which displays 3 tabs: home, popular, all.
Now, I am using context of react to maintain:
activetab which stores current tab.toggleTab method which changes activetab using setState.TabComponent
import React, {Component} from 'react'
import Context from '../../provider'
import {Nav, NavItem, NavLink} from 'reactstrap'
import {Redirect} from 'react-router-dom'
import classnames from 'classnames'
export default class TabComponent extends Component {
    render() {
        return (
            <Context.Consumer>
                {context => (
                    <Nav tabs color='primary'>
                        <NavItem>
                            <NavLink
                                className={classnames({ active: context.activeTab === '1' })}
                                onClick={() =>{context.toggleTab('1')}}
                            >
                            Home
                            </NavLink>
                        </NavItem>
                        <NavItem>
                            <NavLink
                                className={classnames({ active: context.activeTab === '2' })}
                                onClick={() => {context.toggleTab('2')}}
                            >
                            Popular
                            </NavLink>
                        </NavItem>
                        <NavItem>
                            <NavLink
                                className={classnames({ active: context.activeTab === '3' })}
                                onClick={() => {context.toggleTab('3')}}
                            >
                            All
                            </NavLink>
                        </NavItem>                
                    </Nav>
                )}
            </Context.Consumer>
        )
    }
}
What I want to achieve is, onClick should also change the URL.
\home\\popular\\all\Using this.props.history in the onClick method is not working with export default withRouter(TabComponent).
<NavLink
    className={classnames({ active: context.activeTab === '1' })}
    onClick={() =>{
        context.toggleTab('1');
        this.props.history('/home/');
    }}
>
Error:
You should not use
<Route>orwithRouter()outside a<Router>
window.location in onClick:
<NavLink
    className={classnames({ active: context.activeTab === '1' })}
    onClick={() =>{
        context.toggleTab('1');
        window.location = '/home/';
    }}
>
This actually works but the behaviour is not good.
Clicking the tab does the following:
context.toggleTab.window.location.The problem here is the content is already loaded in the 1st step. Using window.location changes the URL but also re-loads the page which is not required. It there a way to do skip the 3rd step or is that any other method which just changes the URL?
Your TabComponent does not have access to the React Router history route prop because it's probably not used as a component given to a Route.
You could use withRouter on your TabComponent to get access to the route props.
class TabComponent extends Component {
    render() {
      // ...
    }
}
export default withRouter(TabComponent);
You also must make sure that you have a Router component at the top of your app.
Example
class App extends Component {
  render() {
    return (
      <BrowserRouter>
        {/* ... */}
      </BrowserRouter>
    );
  }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With