Description
Hi everyone 👋
The time has come to implement an alternative to ReactDOM.findDOMNode
, as explained in React docs:
findDOMNode
is an escape hatch used to access the underlying DOM node. In most cases, use of this escape hatch is discouraged because it pierces the component abstraction. It has been deprecated inStrictMode
.
The goal is to come up with an API that is optional and falls back to ReactDOM.findDOMNode
. I think that we should eventually change the entire API into something more sustainable, probably hooks, but that's a separate discussion.
So far we have three proposed solutions:
1. findDOMNode
, a function that returns a node (#457)
@eps1lon's solution would look like this:
import React from 'react'
import { Transition } from 'react-transition-group'
const StrictModeTransition = () => {
const childRef = React.useRef(null)
return (
<Transition appear in findDOMNode={() => childRef.current}>
<div ref={childRef}>
transition
</div>
</Transition>
)
}
2. domRef
, a ref object (#559)
@iamandrewluca's solution would look like this:
import React from 'react'
import { Transition } from 'react-transition-group'
const StrictModeTransition = () => {
const childRef = React.useRef(null)
return (
<Transition appear in domRef={childRef}>
<div ref={childRef}>
transition
</div>
</Transition>
)
}
3. ref object as a 2nd argument in render prop
My solution would look like this:
import React from 'react'
import { Transition } from 'react-transition-group'
const StrictModeTransition = () => {
return (
<Transition appear in>
{(status, childRef) => (
<div ref={childRef}>
transition
</div>
)}
</Transition>
)
}
So let's begin!
Updates
For those who are just catching up with the discussion, one notable proposition is to extend the 3rd solution with the ability to pass your own ref, which would then be forwarded to the render prop:
import React from 'react'
import { Transition } from 'react-transition-group'
const StrictModeTransition = () => {
const childRef = React.useRef(null)
return (
<Transition appear in ref={childRef}>
{(status, innerRef) => (
// innerRef === childRef
<div ref={innerRef}>
{status}
</div>
)}
</Transition>
)
}
This way we can avoid having to merge refs, which is the key benefit of the 2nd solution. (If you don't pass a ref to Transition
, it would create one internally.)