mobx-react-router icon indicating copy to clipboard operation
mobx-react-router copied to clipboard

Compatible with Server Side Rendering?

Open andrefox333 opened this issue 6 years ago • 2 comments

Anyone using this library with their SSR setup? I keep getting Invariant Violation: Browser history needs a DOM errors with react-router...

andrefox333 avatar Jan 04 '19 18:01 andrefox333

You should use StaticRouter with MemoryHistory for SSR.

dmwmt avatar Mar 19 '19 22:03 dmwmt

I've made it work as follows, let me know if that helps

import {Router, StaticRouter, StaticRouterContext} from "react-router";
import { createMemoryHistory } from 'history';
import {syncHistoryWithStore} from "mobx-react-router";

function reactApp(url: string, apollo: ApolloClient<any>, context: StaticRouterContext = {}) {
    const memoryHistory = createMemoryHistory();
    const history = syncHistoryWithStore(memoryHistory, new RouterStore());
    history.replace(url);

    return (
        <Router history={history}>
            <StaticRouter location={url} context={context}>
                <Application
                    apollo={apollo}
                />
            </StaticRouter>
        </Router>
    );
}

fenos avatar Apr 07 '19 14:04 fenos