8

I moved from react-router to react-router-dom and fixed all the errors. But, now on clicking the links, url is changing but respective view is not rendering.

Here is structure of my application:

index.js

ReactDOM.render(
  <Provider store={createStoreWithMiddleware(reducers)}>
    <HashRouter>
        <App>
            <Switch>
                    <Route path="signin" component={Signin} />
                    <Route path="about" component={About} />
                    <Route path="signout" component={Signout} />
                    <Route path="signup" component={Signup} />
                    <Route path="thanks" component={Thanks} />
                    <Route path="tasks" component={ requireAuth(Task)  }/>
                    <Route path="/" component={ Content }/>

            </Switch>
        </App>
    </HashRouter>
  </Provider>
  , document.querySelector('.container'));

app.js

class App extends Component {
    render() {
        return (
            <div>
                <Header/>
                    {this.props.children}
                <Footer/>               
            </div>
        );
    }
}

export default withRouter(App);

header.js

class Header extends Component {

    showLoginButton() {
        if (this.props.authenticated) {
          // show a link to sign out
          return [<span className="loginBtn">
                    <Link to="/signout">Sign Out</Link>
                    </span>,
                <span className="loginBtn">
                    <Link to="/tasks">Tasks</Link>
                </span>
          ]
        } else {
          // show a link to sign in or sign up
          return [
            <span className="loginBtn" key={1}>
              <Link className="nav-link" to="/signin">Sign In</Link>
            </span>,
            <span className="loginBtn" key={2}>
              <Link className="nav-link" to="/signup">Sign Up</Link>
            </span>,
            <span className="loginBtn" key={3}>
                  <Link className="nav-link" to="/about">About</Link>
              </span>
          ];
        }
    }

    render() {
        return (
            <div className="header">
                <span className="logo">
                    <Link to="/" className="navbar-brand">company</Link>
                </span>
                {this.showLoginButton()}
            </div>
        );
    }
}

function mapStateToProps(state) {
  return {
    authenticated: state.auth.authenticated
  };
}

export default withRouter(connect(mapStateToProps)(Header));

I tried withRouter as mentioned in post https://stackoverflow.com/a/43811162/888181, but still it's not working!

5 Answers 5

15

Try to move root path to the top and make sure you put exact in front of path. Also, you should put / for other paths.

<Switch>
    <Route exact path="/" component={ Content }/>
    <Route path="/signin" component={Signin} />
    <Route path="/about" component={About} />
    <Route path="/signout" component={Signout} />
    <Route path="/signup" component={Signup} />
    <Route path="/thanks" component={Thanks} />
    <Route path="/tasks" component={ requireAuth(Task)  }/>
</Switch>

reference: https://reacttraining.com/react-router/web/api/Switch

Sign up to request clarification or add additional context in comments.

1 Comment

Can you explain the need of "exact" ?
1
import {NavLink} from 'react-router-dom';

use NavLink component instread of a tag, for me it's working

Comments

1

I faced the same problem recently when using react-router-dom V6, all I did to resolve the issue was:

  1. npm uninstall react-router-dom
  2. npm install [email protected]
  3. npm i --save @types/react-router-dom

Then I check my package.json but the dependencies turned out:

"@types/react-router-dom": "^5.3.3",
"react-router-dom": "^5.2.0"

So to the last step:

  1. npm install [email protected]

That's it, my 'Link' worked just fine then.

2 Comments

The problem stated in this answer is not the same as the one in the question, so the answer is not relevant to it.
I faced the problem with v5 and followed the steps, it worked like a charm :), thanks @hadi-learn
0

A bit late now but I recently had the same problem, which is why I came across your question, and found this article helpful: https://reactrouter.com/docs/en/v6/api#route

<Router>
    <Route exact path="/" element={ <Content /> }/>
</Router>

Now react-router-dom v6 has replaced the Switch elements with Routes too, so if you upgrade your package version be aware of that https://reactrouter.com/docs/en/v6/upgrading/v5#upgrade-all-switch-elements-to-routes

Comments

0

I faced the same problem and doing this resolved the issue for me -

  1. Install version 5.2.0 of react-router-dom : npm install [email protected]
  2. delete package-lock.json and node_modules
  3. install all the packages again : npm install

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.