Next.js: Recreate withRouter HOC from next/router using new Context API from React

Created on 8 Oct 2018  路  5Comments  路  Source: vercel/next.js

Bug report

Describe the bug

if we have the next tree of components:
<ComponentWithShouldComponentUpdate> <ComponentWithRouter /> </ComponentWithShouldComponentUpdate>
where ComponentWithShouldComponentUpdate has shouldComponentUpdate which in some cases returns false then ComponentWithRouter doesn't update its context and so ComponentWithRouter has stale router prop :( .

To Reproduce

just render the tree described above

Expected behavior

router prop should be up to date.

Screenshots

no screenshots provided

System information

  • OS: should not depend on OS
  • Browser: should not depend on Browser
  • Version of Next.js: [e.g. 7.0.1]

Additional context

no additional context

feature request

Most helpful comment

I don't think we should spend time on enhancing the current implementation, we should switch to React 16.3 Context for the next major release.

All 5 comments

any news?

It would break compatibility with React version < 16.3. We can enhance current implementation by listening to Router events and forceUpdate subscribed components similar to what react-redux connect does.

I can implement it. I just need to know which events should force update of the subscribed component and how what cases do I need to test to ensure that it will not break anything.

Here's the list of Router events:

  • routeChangeStart(url) - Fires when a route starts to change
  • routeChangeComplete(url) - Fires when a route changed completely
  • routeChangeError(err, url) - Fires when there's an error when changing routes
  • beforeHistoryChange(url) - Fires just before changing the browser's history
  • hashChangeStart(url) - Fires when the hash will change but not the page
  • hashChangeComplete(url) - Fires when the hash has changed but not the page

I don't think we should spend time on enhancing the current implementation, we should switch to React 16.3 Context for the next major release.

Closing in favor of #5716

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timneutkens picture timneutkens  路  3Comments

jesselee34 picture jesselee34  路  3Comments

knipferrc picture knipferrc  路  3Comments

wagerfield picture wagerfield  路  3Comments

rauchg picture rauchg  路  3Comments