-
Notifications
You must be signed in to change notification settings - Fork 27.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
withRouter passes undefined router prop in SSR with decorated pages #2908
Comments
I don't have experience with My problem manifested when trying to integrate Apollo on the server side. I had a code in a higher order component that wrapped the page component that looked like this: const router = {query: context.query, pathname: context.pathname};
const app = (
<ApolloProvider client={apollo}>
<ComposedComponent url={router {...composedInitialProps} />
</ApolloProvider>
);
await getDataFromTree(app); The obvious issue with this (after hitting the wall for an hour :/ ) is that it does not provide Next context to the underlying component structure. So even though I'm using this only to get GraphQL queries and not to render it as HTML, whenever some component is using The solution for me was to provide router context object on the server side like so: const router = {query: context.query, pathname: context.pathname};
const app = (
<RouterProvider router={router}>
<ApolloProvider client={apollo}>
<ComposedComponent url={router} {...composedInitialProps} />
</ApolloProvider>
</RouterProvider>
);
await getDataFromTree(app);
import React from 'react'
import PropTypes from 'prop-types'
class RouterProvider extends React.Component {
getChildContext () {
return {
router: this.props.router
}
}
render () {
return this.props.children
}
}
RouterProvider.childContextTypes = {
router: PropTypes.object.isRequired
}
RouterProvider.propTypes = {
router: PropTypes.object.isRequired,
}
export default RouterProvider Hope this helps someone figure out issues with their code. |
I was trying to do same thing like @jaydenseric style active links :) |
@kyrisu thanks, that was really helpfull for a workaround. Additionally I added const url = {
query: context.query,
pathname: context.pathname,
asPath: context.asPath
} And I had to wrap Surely this can be fixed in Next.js. At the very least, many of the examples using page decorators need to be updated to account for this issue. |
router.asPath is always undefined for custom routes. I can get router.route and router.query, but never full asPath. It is little bit complicated when we use dynamic links build with Express or another backend framework. For example
I could get full path in page component, but sending it through all components is not what i really want. Does exist any way how to push full path from page component to withRoute HOC? |
A simpler way to work around this is to pass the router context as the second argument to await getDataFromTree(
<ApolloProvider client={apollo} store={store}>
<ComposedComponent {...composedInitialProps} />
</ApolloProvider>,
{
router: { query: ctx.query, pathname: ctx.pathname, asPath: ctx.asPath },
},
) This might be related to: apollographql/react-apollo#425 |
@radeno Just fixed the |
Add support for using withRouter as HOC with this example. Passing router context manually fixes this, based on, #2908 (comment)
This has been fixed a while ago. |
The
withRouter
HOC passesundefined
for therouter
prop in SSR with custom routes. The client render works as expected, causing a React server/client checksum error.I use
next-routes
, but others on Slack are having the same issue when usingapp.render
inserver.js
.Context
I am attempting to style active links.
Your Environment
The text was updated successfully, but these errors were encountered: