import React from 'react';
import ReactDOM from 'react-dom';
import registerServiceWorker from './registerServiceWorker';
import Routes from './Routes.js';
import store from './Store.js';
import {Provider} from 'react-redux';
import './css/bootstrap.min.css';
import './css/navbar/chartist-custom.css';
import './css/navbar/main.css';
import './css/navbar/font-awesome.min.css';
import './css/navbar/style.css';
import {createBrowserHistory} from 'history'
import React, {Component} from 'react';
import {
Route,
Switch,
Link,
BrowserRouter,
Router,
Redirect
} from 'react-router-dom';
import LoginPage from './views/pages/LoginPage.js';
import SuccessPage from './views/pages/SuccessPage.js';
import errorPage from './views/pages/errorPage.js';
import store from './Store.js';
如题,我用react-router4做登录的路由,当我登陆成功后,改变store中存储的currentURL,然后Router中监听到store的变化,用this.setState()改变Router的state,用History改变了页面的URL,并试图引起页面组件路由的重新渲染,render确实被调用了,路由却没有渲染新对应URL的组件。
此时如果刷新页面,则会渲染相应的新组件
const history = createBrowserHistory();
ReactDOM.render(
, document.getElementById('root'));
registerServiceWorker();
import React, {Component} from 'react'; import { Route, Switch, Link, BrowserRouter, Router, Redirect } from 'react-router-dom'; import LoginPage from './views/pages/LoginPage.js'; import SuccessPage from './views/pages/SuccessPage.js'; import errorPage from './views/pages/errorPage.js'; import store from './Store.js';
class Routes extends Component {
constructor(props) { super(props); this.URLChange = this.URLChange.bind(this); this.getOwnState = this.getOwnState.bind(this);
}
getOwnState() { return { currentURL: store.getState()["currentURL"] }; }
URLChange() { console.debug(this.getOwnState()["currentURL"]); this.props.history.push(this.getOwnState()["currentURL"]);
}
componentDidMount() { store.subscribe(this.URLChange); }
render() { alert("render:" + JSON.stringify(this.props.history.location.pathname)); return (
} }
export default Routes;
componentWillUpdate() { alert("componentWillUpdate"); } componentDIdUpdate() { alert("componentDIdUpdate"); }
//展示组件 function LoginPage({login}) { alert("LoginPage"); return (
//展示组件 function SuccessPage() { alert("SuccessPage"); return (