简体   繁体   中英

Redirect to Route using react-router

I'm using react-router in my ReactJS application. I'm trying to redirect a particular route using react-router . Here is the code:

ReactDOM.render(
    <Router history={hashHistory}>
        <Route component={ShowPost} path="/"></Route>
        <Route component={AddPost} path="/addPost"></Route>
    </Router>,
    document.getElementById('app')
);

But I really can't figure out how to redirect to a particular route programatically.

You just have to import Link from 'react-router'.

See https://github.com/reactjs/react-router-tutorial/tree/master/lessons/05-active-links

<Link to="/" > Home </Link>
<Link to="/addPost"> Add Post </Link>

If you mean Redirect you can check https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/Redirect.md

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM