javascript - React-Router keeps giving me a warning: You cannot change <Router routes> - Stack Overflow

I am using React-Router on my application with a personalized history object.It looks like this:import

I am using React-Router on my application with a personalized history object.

It looks like this:

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

...

ponentWillMount() {
    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="/" ponent={Sign}>
                <IndexRedirect to="/login" />
                <Route path="login" ponent={Login} />
            </Route>
        </Router>
    );
}

So when I access mywebsite/sign, it redirects me to mywebsite/sign/login which is fine, but I get this error in the console:

Warning: [react-router] You cannot change <Router routes>; it will be ignored

If I access the login page directly, I don't get any error.

Any idea what's wrong?

Thanks

I am using React-Router on my application with a personalized history object.

It looks like this:

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

...

ponentWillMount() {
    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="/" ponent={Sign}>
                <IndexRedirect to="/login" />
                <Route path="login" ponent={Login} />
            </Route>
        </Router>
    );
}

So when I access mywebsite./sign, it redirects me to mywebsite./sign/login which is fine, but I get this error in the console:

Warning: [react-router] You cannot change <Router routes>; it will be ignored

If I access the login page directly, I don't get any error.

Any idea what's wrong?

Thanks

Share Improve this question edited Aug 28, 2018 at 9:41 Gaurav Paliwal 1,59616 silver badges28 bronze badges asked May 10, 2016 at 20:29 alexmngnalexmngn 9,66720 gold badges75 silver badges136 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

That's probably happening because your "router ponent" is trying to re-render everytime something changes (probably the history).

It's easier to use a const for your routes

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

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

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744962050a4603446.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信