admin管理员组文章数量:1294071
In the header, I have a menu button, that when clicked will display different links to go to. However, I would only like to show the menu button when its in the home path (i.e., "/"). And when I navigate to other pages, I would like to change the the menu button to back button. This back button should be like the browser back button which will go back one step at a time, till I get back to the home path. How can I achieve this? I am using "react": "^15.1.0" and "react-router": "^2.5.2".
AppClient.js
ReactDom.render((
<Router history={hashHistory} >
<Route path="/" ponent={App}>
<IndexRoute ponent={Home} />
<Route path="home" ponent={Home}/>
...
...
<Route path="profile" ponent={Profile}>
<IndexRoute ponent={Timeline} />
<Route path="timeline" ponent={Timeline}/>
</Route>
<Route path="login" ponent={Login}/>
</Router>
), reactContainer)
App.js
export default class App extends React.Component {
render() {
const _this = this;
return (
<div>
<Header/>
...
...
</div>
);
}
}
Header.js
export default class Header extends React.Component {
render() {
return(
<header>
<div id="header-wrapper">
<div id="nav-bar-btn" class="nav">
// change Menu when its not home path that is "/"
<Menu>
// to Back
<Back>
</div>
</div>
</header>
);
}
}
In the header, I have a menu button, that when clicked will display different links to go to. However, I would only like to show the menu button when its in the home path (i.e., "/"). And when I navigate to other pages, I would like to change the the menu button to back button. This back button should be like the browser back button which will go back one step at a time, till I get back to the home path. How can I achieve this? I am using "react": "^15.1.0" and "react-router": "^2.5.2".
AppClient.js
ReactDom.render((
<Router history={hashHistory} >
<Route path="/" ponent={App}>
<IndexRoute ponent={Home} />
<Route path="home" ponent={Home}/>
...
...
<Route path="profile" ponent={Profile}>
<IndexRoute ponent={Timeline} />
<Route path="timeline" ponent={Timeline}/>
</Route>
<Route path="login" ponent={Login}/>
</Router>
), reactContainer)
App.js
export default class App extends React.Component {
render() {
const _this = this;
return (
<div>
<Header/>
...
...
</div>
);
}
}
Header.js
export default class Header extends React.Component {
render() {
return(
<header>
<div id="header-wrapper">
<div id="nav-bar-btn" class="nav">
// change Menu when its not home path that is "/"
<Menu>
// to Back
<Back>
</div>
</div>
</header>
);
}
}
Share
Improve this question
asked Jul 20, 2016 at 10:01
Benjamin Smith MaxBenjamin Smith Max
2,74810 gold badges36 silver badges57 bronze badges
3
-
Use
<input type="button" value="Back" onclick="history.go(-1);" />
– mmushtaq Commented Jul 20, 2016 at 10:10 - This is HTML syntax.. – mmushtaq Commented Jul 20, 2016 at 10:14
-
if you are using redux too, then you'll get a router through props and then you can simple check for the router.location.pathname use ternary operator to mount the
<Menu />
or<Back />
ponent. Other wise you'll have to listen for route change. – Vikramaditya Commented Jul 20, 2016 at 10:43
2 Answers
Reset to default 4jsfiddle demo
import { hashHistory } from 'react-router';
export default class Header extends React.Component {
constructor(props){
super(props);
this.state={showBack:location.hash.split('?')[0]!=="#/"};
this.hook = hashHistory.listenBefore(loc=>
this.setState({showBack:loc.pathname!=="/"})
);
}
ponentWillUnmount(){
this.hook(); //unlisten
}
render() {
return(
<header>
<div id="header-wrapper">
<div id="nav-bar-btn" class="nav">
{this.state.showBack?
<div onClick={()=>hashHistory.goBack()}>Go BACK</div>
: <Menu/>
}
</div>
</div>
</header>
);
}
}
listenBefore
watches path, and decides whether show button or not.
And hashHistory.goBack()
, works like browser back button
You need to add the router to your context (https://facebook.github.io/react/docs/context.html).
Header.contextTypes = {
router: React.PropTypes.object.isRequired
};
react-router will normally populate the this.context.router variable.
With this this.context.router object, there's a lot of methods available, like goBack() (https://github./reactjs/react-router/blob/master/docs/API.md#goback).
版权声明:本文标题:javascript - Display Back button to go back like the browser back button when not in home path - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741593064a2387247.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论