React.StrictMode throws warning because of `findDOMNode` use
See original GitHub issueWarning: findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of StyledComponent which is inside StrictMode. Instead, add a ref directly to the element you want to reference.
I am aware that it is only run in development-mode, but it clutters console output when trying to find errors.
@Fer0x Could we change this here somehow? (by not using findDOMNode)
I don’t think it is a problem, not even sure if there is a proper solution. Keeping it for later reference.
_Originally posted by @optunetobi in https://github.com/styled-components/styled-components/pull/2073/review_comment/create_
Issue Analytics
- State:
- Created 5 years ago
- Reactions:39
- Comments:62 (30 by maintainers)
Top Results From Across the Web
Warning: findDOMNode is deprecated in StrictMode ...
The solution to resolve the warning is by using React refs. Refs helps to access DOM nodes or React elements created in the...
Read more >React warning: findDOMNode is deprecated in StrictMode ...
The Problem When working with React.js, you will probably come across an annoying warning: Warning: findDOMNode is deprecated in StrictMode.
Read more >Strict Mode - React
Identifying components with unsafe lifecycles; Warning about legacy string ref API usage; Warning about deprecated findDOMNode usage; Detecting unexpected ...
Read more >findDOMNode is deprecated in StrictMode, react-redux-notify ...
This is an error probably from a library you are using to say that the ReactDOM.findDOMNode function is now deprecated but it still...
Read more >How to use React Strict Mode in React 18 - Refine Dev
Warning about deprecated findDOMNode usage ... FindDOMNode was a React feature designed to search the DOM node tree for a specific class ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Big thanks to @Fer0x for actually explaining what
styled-components
is doing withfindDOMNode
and why — which is missing from this thread and made it difficult to respond to it initially. For future cases where you feel React is getting in your way I’d appreciate if you could provide similar explainers. This was very helpful.This approach seems a bit flawed to me because the general expectation is that it should be valid for a child component to not render the node with a class name (e.g. due to some early exit condition) and maybe render it later. So it’s a heavy handed assumption that I must render that
className
regardless.Of course I did not design SC API and don’t have context on your decisions — I’m just looking from the point of view of a React component user. I generally expect that I can add
return null
into any component at any point in time and it shouldn’t cause me to see warnings. It’s a valid React feature.Fragments present a different issue (
findDOMNode
will only find the first node) that can also cause misleading warnings. So I think we can agree the warning seems to stand in the way of using normal React features somewhat.Now, maybe the mistake is very common and so the benefits of having a warning outweigh the false positives and other issues. I would argue that the issue might be inherent to this API that “pretends“ to override component’s styles with
styled(Button)
and might give people the wrong mental model. But maybe that’s also part of the appeal of styled components — it feels more familiar than e.g. CSS-in-JS, andstyled(Button)
kinda feels like selector overrides.One thing you could do is to make
className
an object with customtoString
that returns a class name in DEV. React would (AFAIK)toString
it before rendering. That would let you detect thattoString
was called. If it’s not called within some timeout, you could warn in DEV. In production you could still use strings.Yet another thing you could is to add a unique identifier to the
className
in DEV. (So, effectively, it would be two class names — one for the component and one for the instance — separated by spaces.) Then you could do thedocument.querySelector
check suggested in https://github.com/styled-components/styled-components/issues/2154#issuecomment-444329164 without worrying about conflicts. Seems like that would be the easiest fix unless I’m missing something.Hope that helps. As always, we’re happy to support you and try to find solutions to problems you encounter. But since we don’t use
styled-components
it’s difficult to understand the challenges you’re running into until we get a nice explanation like https://github.com/facebook/react/issues/14357#issuecomment-444045535. Instead of arguing whether the API needs to be brought back or not I think it would be much more productive if we always frame a discussion about a specific use case you need to solve.Please file issues for those use cases. No solid proposal will be raised if those use cases are only described in other repos, and nobody comes to file them to ours. 😃