react: Bug: `createRoot` function from React@18 break the css `:target`

Keywords

  • React 18
  • createRoot
  • CSS selector
  • CSS Target
  • :target

React version: React@18

Steps To Reproduce

  1. open https://joyful-kelpie-c3bb20.netlify.app/buggy.html
  2. click go to react id target link
  3. You should see the react id target with green background
  4. Refresh page. The green background disappears

Link to code example:

The webpage source code is simple. You could check the source code directly.

Or, you can check this: https://gist.github.com/magic-akari/475a13219394fb938cff4169a9b61eb7

The current behavior

CSS target selector does not work. The green background disappears after refresh pages.

The expected behavior

CSS target selector should work after refresh pages.

See: https://joyful-kelpie-c3bb20.netlify.app/ok.html

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 3
  • Comments: 15

Most upvoted comments

I’ve tested it in Chrome, Firefox, and Safari. Chrome and Safari show reported behavior, and Firefox doesn’t. The same goes for any Canary.

Just for convenience, the main difference between ok.html and buggy.html is shown below.

-     ReactDOM.render(
-       React.createElement(App),
-       document.querySelector("#root")
-     );
+     const rootElement = document.querySelector("#root");
+     const root = ReactDOM.createRoot(rootElement);
+     root.render(React.createElement(App));

I’ve seen the same behavior. An issue I found that seems similar to me was https://github.com/remix-run/history/issues/503 , but history state is a bit new to me and I’m not sure if it’s related.

I checked the source and it calls window.history.pushState with the new URL when history API is available, which doesn’t seem to trigger the :targer selector.

Edit: From other searching, it looks like this may be related to this whatwg issue: https://github.com/whatwg/html/issues/639#issuecomment-1030914480. It seems like they’re going to keep it that way.