Javascript React Router一直在警告我:你不能改变<;路由器路由>;

Javascript React Router一直在警告我:你不能改变<;路由器路由>;,javascript,reactjs,react-router,Javascript,Reactjs,React Router,我正在我的应用程序上使用React Router和一个个性化的历史对象 看起来是这样的: import { createHistory } from 'history'; import { Router, Route, IndexRedirect, useRouterHistory } from 'react-router'; import { syncHistoryWithStore } from 'react-router-redux'; ... componentWillMount()

我正在我的应用程序上使用React Router和一个个性化的历史对象

看起来是这样的:

import { createHistory } from 'history';
import { Router, Route, IndexRedirect, useRouterHistory } from 'react-router';
import { syncHistoryWithStore } from 'react-router-redux';

...

componentWillMount() {
    const browserHistory = useRouterHistory(createHistory)({
        basename: '/sign',
    });

    this.history = syncHistoryWithStore(browserHistory, store, {
        selectLocationState: (state) => state.routing,
    });
}

render() {
    const history = this.history;

    return (
        <Router history={history}>
            <Route path="/" component={Sign}>
                <IndexRedirect to="/login" />
                <Route path="login" component={Login} />
            </Route>
        </Router>
    );
}
如果我直接访问登录页面,我不会得到任何错误

知道怎么了吗


谢谢

这很可能发生,因为您的“路由器组件”每次都试图重新呈现某些更改(可能是历史记录)

在路线上使用常量更容易

const browserHistory = useRouterHistory(createHistory)({
    basename: '/sign',
});

const history = syncHistoryWithStore(browserHistory, store, {
    selectLocationState: (state) => state.routing,
});

const routes = (<Route path="/" component={Sign}>
                <IndexRedirect to="/login" />
                <Route path="login" component={Login} />
            </Route>)

ReactDOM.render(
  <Router history={history}>
    {routes}
  </Router>,
  yourElement
);
const browserHistory=useRouterHistory(createHistory)({
basename:“/符号”,
});
const history=syncHistoryWithStore(浏览器历史记录、存储、{
选择LocationState:(state)=>state.routing,
});
常数路由=(
)
ReactDOM.render(
{routes}
,
你的元素
);
const browserHistory = useRouterHistory(createHistory)({
    basename: '/sign',
});

const history = syncHistoryWithStore(browserHistory, store, {
    selectLocationState: (state) => state.routing,
});

const routes = (<Route path="/" component={Sign}>
                <IndexRedirect to="/login" />
                <Route path="login" component={Login} />
            </Route>)

ReactDOM.render(
  <Router history={history}>
    {routes}
  </Router>,
  yourElement
);