Sij*_*tha 10 reactjs react-router
我正在使用react-route,我在路由方面遇到了一些麻烦.
整个页面正在重新加载,导致我已经获取并存储在reducer中的所有数据每次都加载.
这是我的Route文件:
var CustomRoute = React.createClass({
render:function(){
return <Router history={browserHistory}>
<Route path="/" component={Layout}>
<IndexRedirect to="/landing" />
<Route path="landing" component={Landing} />
<Route path="login" component={Login} />
<Route path="form" component={Form} />
<Route path="*" component={NoMatch} />
</Route>
</Router>
},
});
Run Code Online (Sandbox Code Playgroud)
在路由之前,我已经通过调用main.jsx中的action来存储数据
/** @jsx React.DOM */
'use strict'
var ReactDOM = require('react-dom')
var React = require('react')
var Index = require('./components/index')
var createStore = require("redux").createStore
var applyMiddleware = require("redux").applyMiddleware
var Provider = require("react-redux").Provider
var thunkMiddleware = require("redux-thunk").default
var reducer = require("./reducers")
var injectTapEventPlugin =require('react-tap-event-plugin');
injectTapEventPlugin()
var createStoreWithMiddleware=applyMiddleware(thunkMiddleware)(createStore);
var store=createStoreWithMiddleware(reducer);
store.dispatch(actions.load_contacts()) //////////// <<<<<< this is what i call to store data already
ReactDOM.render( <Provider store={store}><Index/></Provider> , document.getElementById('content'))
Run Code Online (Sandbox Code Playgroud)
问题是如果成功登录,我必须路由到另一个页面:
this.props.dispatch(actions.login(this.state.login,this.state.password,(err)=>{
this.setState({err:err})
if (!err){
window.location = "/form"
}
}));
Run Code Online (Sandbox Code Playgroud)
该window.location的是卓有成效的,但它重新加载一切,这是非常低效的.在手动路线我使用<Link\>该路线而不重新加载,但是为了自动化,我不知道如何做.
任何建议将不胜感激.
Voč*_*čko 15
React >= 16.8 和 React-router v4
没有人提到使用钩子的应用程序的解决方案。从React 16.8(和react-router 4,不确定最小版本是什么,我使用最新版本)开始,你可以使用一个名为useHistory的钩子。
import { useHistory } from 'react-router';
const MyHookComponent: React.FC = () => {
const routerHistory = useHistory();
routerHistory.push('/page-where-to-redirect');
return <></>;
}
export default MyHookComponent;
Run Code Online (Sandbox Code Playgroud)
更多信息请参见https://reactrouter.com/web/api/Hooks/。
更新:react-router v6
React-router 6 中引入了一个新的钩子:
import { useHistory } from 'react-router';
const MyHookComponent: React.FC = () => {
const routerHistory = useHistory();
routerHistory.push('/page-where-to-redirect');
return <></>;
}
export default MyHookComponent;
Run Code Online (Sandbox Code Playgroud)
文档可以在这里找到https://reactrouterdotcom.fly.dev/docs/en/v6/api#usenavigate
小智 12
这是唯一对我有用的事情:
window.history.pushState("", "", "/new-url");
Run Code Online (Sandbox Code Playgroud)
或者替换:
window.history.replaceState("", "", "/new-url");
Run Code Online (Sandbox Code Playgroud)
Shu*_*tri 10
对于最新版本(v2.0.0-rc5),推荐的导航方法是直接推送到历史单例.
相关代码
import { browserHistory } from './react-router'
browserHistory.push('/some/path')
Run Code Online (Sandbox Code Playgroud)
如果使用较新的react-router API,则需要在组件内部使用historyfrom this.props,这样:
static propTypes = {
history: React.PropTypes.object
}
this.props.history.push('/some/path');
Run Code Online (Sandbox Code Playgroud)
如果使用react-router-redux,它提供了一个push你可以调度的功能:
import { push } from 'react-router-redux';
this.props.dispatch(push('/some/path'));
Run Code Online (Sandbox Code Playgroud)
在v2.4.0及更高版本中,使用更高阶的组件将路由器作为组件的支柱.
import { withRouter } from 'react-router'
class Example extends React.Component {
// use `this.props.router.push('/some/path')` here
};
// Export the decorated class
var DecoratedExample = withRouter(Example);
// PropTypes
Example.propTypes = {
router: React.PropTypes.shape({
push: React.PropTypes.func.isRequired
}).isRequired
};
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请访问此链接:http://kechengpuzi.com/q/s31079081
| 归档时间: |
|
| 查看次数: |
14229 次 |
| 最近记录: |