3

I have the following code to render a filtered list with animated mounting/unmounting:

function List({ list, templates, transition }) {
  return (
    <TransitionGroup 
        className="wscfl-list"
        component="ul"
    >
    {list.map((item) => (
      <CSSTransition
        key={item.id}
        timeout={transition.timeout}
        classNames={'wscfl-list__' + item.type + '-'}
      >
        <li className={'wscfl-list__' + item.type} >
          <Item item={item} template={templates[item.type]} pkey={item.id}/>
        </li>
      </CSSTransition>
    ))}
    </TransitionGroup>
  );
}

This will throw an 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. which I would like to avoid.

I know I'm supposed to the add a ref to the CSSTransition (https://github.com/reactjs/react-transition-group/releases/tag/v4.4.0):

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>
  )
}

However I cannot use useRef inside the map callback since it's not allowed to use hooks in a callback.

I tried to create an array of refs (How target DOM with react useRef in map):

function List({ list, templates, transition }) {
  // https://stackoverflow.com/questions/54940399/how-target-dom-with-react-useref-in-map/55105849
  const refsArray = [];
  for (var i= 0; i < list.length; i++) {
    refsArray.push(React.createRef());
  }
  let refs = React.useRef(refsArray);
  React.useEffect(() => {
    refs.current[0].current.focus()
  }, []);
  return (
    <TransitionGroup 
        className="wscfl-list"
        component="ul"
    >
    {list.map((item, i) => (
      <CSSTransition
        key={item.id}
        nodeRef={refs.current[i]}
        timeout={transition.timeout}
        classNames={'wscfl-list__' + item.type + '-'}
      >
        <li ref={refs.current[i]} className={'wscfl-list__' + item.type} >
          <Item item={item} template={templates[item.type]} pkey={item.id}/>
        </li>
      </CSSTransition>
    ))}
    </TransitionGroup>
  );
}

But this will throw an error on first render TypeError: Cannot read property 'current' of undefined.

   9 | }
  10 | let refs = React.useRef(refsArray);
  11 | React.useEffect(() => {
> 12 |   refs.current[0].current.focus()
     | ^  13 | }, []);
  14 | return (
  15 |   <TransitionGroup 

Is there any way to use CSSTransition for mapped components without using findDOMNode?

TeeBoek
  • 33
  • 4

1 Answers1

3

The simplest way to handle this without having the need to use an array of refs is by extracting out the mapped content within another component

const Content = ({item, transition, templates, ...rest}) =>  {
  const nodeRef = React.useRef(null)
  return(
    <CSSTransition
        {...rest}
        nodeRef={nodeRef}
        timeout={transition.timeout}
        classNames={'wscfl-list__' + item.type + '-'}
      >
        <li ref={nodeRef} className={'wscfl-list__' + item.type} >
          <Item item={item} template={templates[item.type]} pkey={item.id}/>
        </li>
      </CSSTransition>

   )
}

function List({ list, templates, transition }) {
  return (
    <TransitionGroup 
        className="wscfl-list"
        component="ul"
    >
    {list.map((item) => (
        <Content item={item} key={item.id} templates={templates} transition={transition}/>
    ))}
    </TransitionGroup>
  );
}
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
  • Thanks, but this will break animation. I suppose the CSSTransition has to be a direct descendant of the TransitionGroup? – TeeBoek Mar 12 '21 at 06:40
  • @TeeBoek it is not a necessity. TransitionGroup passes certain props to its children which you can forward using rest syntax to CSSTransition. I have edited my answer. Also check this same Codesandbox: https://codesandbox.io/s/gracious-gould-iktqw?file=/index.js in which I have tested this – Shubham Khatri Mar 12 '21 at 07:08
  • That is good to know! I had to modify your code and lift up `key={item.id}` to the Content component to avoid the unique key warning though. – TeeBoek Mar 12 '21 at 09:02
  • @TeeBoek Yeah I missed the key thing while copy pasting. Glad the answer helped though – Shubham Khatri Mar 12 '21 at 09:07
  • Thank you man, after one day of countless tries problem solved – Eran Or Aug 12 '21 at 11:34