React Router v4 doesn't work properly with Preact X
- [x] Check if updating to the latest Preact version resolves the issue
Describe the bug React Router v4.x.x doesn't work properly with Preact X latest version (v10.5.15) but works with Preact v8.x.x version.
To Reproduce Minimal example: https://codesandbox.io/s/confident-ives-j9by2?file=/src/index.js
Steps to reproduce the behavior:
- Go to https://codesandbox.io/s/confident-ives-j9by2?file=/src/index.js
- Click on link 'To First' or 'To Second'
- Routing doesn't work. Related components are not rendered
Expected behavior Routing should work properly. 'First' component should be rendered after clicking on the 'To First' link and 'Second' component should be rendered after clicking on the 'To Second' link.
Could anyone look in here?
Hi @VintorezVS - sorry for the delay. I spent some time stepping through your example, and I haven't yet been able to figure out the issue. My theory is that this relates to VNode equality bailout, which is an optimization that was not present in Preact 8.
So far all I have determined is that this broke in Preact 10.4.0 - 10.3.4 works correctly in your demo. Looking at the changes between 10.3.4 and 10.4.0, there are the two PRs that could have altered behaviors relating to the dropped setState seen in this demo: #2386 - exits out of rendering when encountering a JSX element that exactly matches the previous render #2420 - changes how default context works (note: react-router v4/v5 use a custom createContext polyfill, so this one seems unlikely)
I'm fairly confident this issue relates to #2386. It's possible there's a bypass condition React Router is relying on here that is present in React, but that we didn't implement in Preact - if so I'd like to find and fix that.
(modified sandbox for reference)
@VintorezVS Alright, so @JoviDeCroock found the issue! React Router v4 and v5 are API-compatible, and everyone is recommended to upgrade to v5. There is a bug in React Router v4 caused by failure to use cloneElement() when returning identical trees (in Router and Route), and that bug was fixed in v5.
Your sandbox works when updated to RR5: https://codesandbox.io/s/bold-sutherland-nis8n?file=/src/index.js
@developit Thank you for investigation. The workaround works well.