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.historyprops.
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');historythis.props
this.props.history.push('/some/path');pushState
react-router-reduxpush
import { push } from 'react-router-redux';this.props.dispatch(push('/some/path'));添加回答
舉報
