acdlite / redux-router

Redux bindings for React Router – keep your router state inside your Redux store
MIT License
2.3k stars 216 forks source link

onEnter dispatch could work on server rendering? #236

Closed ghost closed 8 years ago

ghost commented 8 years ago

thats my server matching and rendering (from the official server-rendering example):

app.use((req, res) => {
    const store = reduxReactRouter({ routes, createHistory: createMemoryHistory })(createStore)(reducer);
    const query = qs.stringify(req.query);
    const url = req.path + (query.length ? '?' + query : '');

    store.dispatch(match(url, (error, redirectLocation, routerState) => {
        if (error) {
            console.error('Router error:', error);
            res.status(500).send(error.message);
        } else if (redirectLocation) {
            res.redirect(302, redirectLocation.pathname + redirectLocation.search);
        } else if (!routerState) {
            res.status(400).send('Not Found');
        } else {
            res.status(200).send(getMarkup(store));
        }
    }));
});

any my routes looks like this:

export default function ({ dispatch, getState }) {
    function getUser(nextState, replaceState) {
        dispatch(getUserData(nextState.params.id));
    }

    return (
        <Route path="/" component={App}>
            <Route path="user/:id" component={User} onEnter={getUser}/>
                <Route path="*" component={NoMatch}/>
            </Route>
        );
    }
}

i would like to get my markup rendered-to-string after the getUserData dispatch is back with data. it works on the client, but i don't know what to pass to 'routes' in reduxReactRouter({ routes, createHistory: createMemoryHistory })...

mjrussell commented 8 years ago

@SkateFreak you can also use the getRoutes param key instead of routes. So change it to:

import getRoutes from './routes';
`reduxReactRouter({ getRoutes, createHistory: createMemoryHistory })`

Assuming your second js snippet is in the file routes.js