react-transition-group icon indicating copy to clipboard operation
react-transition-group copied to clipboard

CSSTransition using findDOMNode which is deprecated in React 16.13.1

Open david-forster10 opened this issue 6 years ago โ€ข 40 comments

A warning regarding the deprecation of findDOMNode is shown when using the SwitchTransition & CSSTransition in Strict mode (React v16.13.1).

What is the current behaviour?

Warning: findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of Transition which is inside StrictMode. Instead, add a ref directly to the element you want to reference. Learn more about using refs safely here: https://fb.me/react-strict-mode-find-node

What is the expected behavior?

No console messages

Could you provide a CodeSandbox demo reproducing the bug?

Can be recreated using the demo sandbox and changing the React version to 16.13.1. Screenshot (7)

david-forster10 avatar Sep 11 '20 12:09 david-forster10

@david-forster10 Version 4.4.0 have already resolved using findDOMNode.

mznet avatar Sep 14 '20 15:09 mznet

@mznet Running on the same CodeSandbox, v4.4.0 same result Screenshot (8)

david-forster10 avatar Sep 15 '20 11:09 david-forster10

Looking at the stack trace, the issue is actually occurring at the Transition object which is created by the CSSTransition. There are 3 references to the findDOMNode method still in that file:

Line 229

Line 260

Line 321

david-forster10 avatar Sep 15 '20 11:09 david-forster10

Looking at the stack trace, the issue is actually occurring at the Transition object which is created by the CSSTransition. There are 3 references to the findDOMNode method still in that file:

Line 229

Line 260

Line 321

Adding in some cheap logging to find out where in the flow the error is thrown... I would guess it's Line 229 as the error is thrown before the transitions finish.

Screenshot (9)

david-forster10 avatar Sep 15 '20 11:09 david-forster10

I'm just wondering why would close the issue #429 when it is not resolved even with 4.4.1?

image

nt4f04uNd avatar Sep 17 '20 11:09 nt4f04uNd

From 4.4.0 release notes:

react-transition-group internally uses findDOMNode, which is deprecated and produces warnings in Strict Mode, so now you can optionally pass nodeRef to Transition and CSSTransition, it's a ref object that should point to the transitioning child:

You can fix this like this

import React from "react"
import { CSSTransition } from "react-transition-group"

const MyComponent = () => {
  const nodeRef = React.useRef(null)
  return (
    <CSSTransition nodeRef={nodeRef} in timeout={200} classNames="fade">
      <div ref={nodeRef}>Fade</div>
    </CSSTransition>
  )
}

pixel-fixer avatar Sep 19 '20 04:09 pixel-fixer

I think the library must show it's own warning about this and not confuse people and make them search for the solution by themselves

nt4f04uNd avatar Sep 19 '20 12:09 nt4f04uNd

I think to also avoid confusion, the demos (using the older version of the library) need to be updated to show the use of the refs to further reinforce the idea for people who skim the docs

david-forster10 avatar Sep 20 '20 11:09 david-forster10

@jquense please consider our suggestion

nt4f04uNd avatar Sep 20 '20 12:09 nt4f04uNd

The solution proposed by @jquense removes warning, but also prevents animation to play.

ahmedzubaca avatar Dec 24 '20 15:12 ahmedzubaca

the solution of @pixel-fixer works for me, but when adding ref to a complex child component it will not work, you need to wrap your complex component with a div then the transition will play with no errors๐Ÿ™Œ๐Ÿผ. <Transition nodeRef={nodeRef} key={id} timeout={{ enter: 800, exit: 500 }}> <div ref={nodeRef}> ๐Ÿ‘ˆ๐Ÿผ <CardItem/> </div> </Transition>

Samk13 avatar Mar 04 '21 22:03 Samk13

Smak13, thank You so much, this works for me, too.

ahmedzubaca avatar Mar 12 '21 17:03 ahmedzubaca

Thanks Smak13, it works. What a shame this library needs hacks like this...

Szlavicsek avatar Apr 17 '21 16:04 Szlavicsek

Thanks @Samk13 ๐Ÿ™Œ๐Ÿผ

italodom avatar Apr 27 '21 01:04 italodom

thanks a lot @pixel-fixer. This works for me!

nguyentran-se avatar Jul 30 '21 07:07 nguyentran-se

@jquense Why is this closed? The "fix" from pixel-fixer is not a fix but a workaround, that doesn't work in all cases (e.g. it breaks the animation for me and other developers as you can read above). It's also not fixed in 4.4.2 Please re-open

emkayy avatar Aug 03 '21 14:08 emkayy

Not fixed, when I add image

I got something like a piece of unknown

image

It slides or pops up without applying exit animations

talkenson avatar Aug 10 '21 18:08 talkenson

@talkenson Is it possibly because there is styling on that div with the ref? I tried "w-full h-full" given 100vw and 100vh respectively on my own app, it looked something like yours.

jiaxfrank avatar Sep 30 '21 14:09 jiaxfrank

Jip, this does not work, the nodeRef just removes the animation.

aswanevelder avatar Oct 04 '21 07:10 aswanevelder

Jip, this does not work, the nodeRef just removes the animation.

You need to have a unique ref for all your CssTranstion.

<CssTransition nodeRef={ref1}>
     <div ref={ref1}>Fade</div>
</CssTransition>

<CssTransition nodeRef={ref2}>
    <div ref={ref2}>Fade</div>
</CssTransition>

iphyman avatar Oct 09 '21 08:10 iphyman