Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-router-dom Link not working

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!

like image 432
Yogesh Avatar asked Jun 10 '17 18:06

Yogesh


People also ask

How does react router Dom link work?

A <Link> is an element that lets the user navigate to another page by clicking or tapping on it. In react-router-dom , a <Link> renders an accessible <a> element with a real href that points to the resource it's linking to. This means that things like right-clicking a <Link> work as you'd expect.

How do I link to an external URL in react?

You can use the Link component or an HTML anchor tag if you want to redirect to an external link with React Router. Using the < Link > component, you can do it thanks to the pathname and target parameters. It will redirect the user to the specified URL.

How do you pass data in link in react Dom router?

To recap, if you need to pass data from Link through to the new component that's being rendered, pass Link s a state prop with the data you want to pass through. Then, to access the Link s state property from the component that's being rendered, use the useLocation Hook to get access to location.

Is react Dom the same as react router Dom?

React Router DOM: the Difference. react-router is the core package containing standard components and functionalities to implement routing in React applications. On the other hand, react-router-dom is a specialized package that you can use only in web-browser-based application development.


2 Answers

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

like image 137
sam Avatar answered Oct 02 '22 11:10

sam


import {NavLink} from 'react-router-dom';

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

like image 24
user15675581 Avatar answered Oct 02 '22 11:10

user15675581