3 回答
TA贡献1777条经验 获得超3个赞
Reaction-路由器4.0.0+
回答
class Example extends React.Component { // use `this.props.history.push('/some/path')` here};
<Route>
<Route path="..." component={YourComponent}/>
Reaction-路由器3.0.0+
回答
class Example extends React.Component { // use `this.props.router.push('/some/path')` here};
Reaction-路由器2.4.0+
回答
import { withRouter } from 'react-router';class Example extends React.Component { // use `this.props.router.push('/some/path')` here};// Export the decorated classvar DecoratedExample = withRouter(Example); // PropTypesExample.propTypes = { router: React.PropTypes.shape({ push: React.PropTypes.func.isRequired }).isRequired};
Reaction-路由器2.0.0+
回答
import { browserHistory } from 'react-router'
browserHistory.push('/some/path')
Reaction-路由器1.x.x
回答
pushState()
var Example = React.createClass({ mixins: [ History ], navigateToHelpPage () { this.history.pushState(null, `/help`); }})
History
this.props.history
props
.
history = createHistory()
replaceState
Reaction-路由器0.13.x
回答
import React from 'react';import {Navigation} from 'react-router';let Authentication = React.createClass({ mixins: [Navigation], handleClick(e) { e.preventDefault(); this.transitionTo('/'); }, render(){ return (<div onClick={this.handleClick}>Click me!</div>); }});
transitionTo()
.context
class
import React from 'react';export default class Authentication extends React.Component { constructor(props) { super(props); this.handleClick = this.handleClick.bind(this); } handleClick(e) { e.preventDefault(); this.context.router.transitionTo('/'); } render(){ return (<div onClick={this.handleClick}>Click me!</div>); }}Authentication.contextTypes = { router: React.PropTypes.func.isRequired};
Reaction-路由器-Redux
注:
如果您正在使用Redux,则有另一个项目名为 Reaction-路由器-Redux 这为您提供了React路由器的Redux绑定,使用的方法与 反应-剩余 是吗?
push(location)
replace(location)
go(number)
goBack()
goForward()
./actioncreators.js
import { goBack } from 'react-router-redux'export const onBackPress = () => (dispatch) => dispatch(goBack())
./viewComponent.js
<button disabled={submitting} className="cancel_button" onClick={(e) => { e.preventDefault() this.props.onBackPress() }}> CANCEL</button>
TA贡献1802条经验 获得超5个赞
反应-路由器v2
v2.0.0-rc5
import { browserHistory } from 'react-router';browserHistory.push('/some/path');
history
this.props
this.props.history.push('/some/path');
pushState
react-router-redux
push
import { push } from 'react-router-redux';this.props.dispatch(push('/some/path'));
添加回答
举报