waning: 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.
我看了一下对应的issue:https://github.com/reactjs/react-transition-group/issues/668
然后将Transition中的组件代码按照里面的解决方案,在CSSTransition中添加上了一个nodeRef,如下所示,
const Transition: React.FC<TransitionProps> = (props) => {
const { children, classNames, animation, wrapper, ...restProps } = props;
const nodeRef = React.useRef(null);
return (
<CSSTransition
nodeRef={nodeRef}
classNames={classNames ? classNames : animation}
{...restProps}
>
{wrapper ? <div ref={nodeRef}>{children}</div> : children}
</CSSTransition>
);
};
这样子一试,这个警告就消失了,但是也出现了一个问题,就是如果不使用wrapper,根本不会起到动画效果。
但是给每个使用Transition的地方都去添加一个wrapper会不合适,比如在submenu中。因为添加了wrapper,多了一层div会出现下拉框抖动,所以我只能改成用下面代码。
const Transition: React.FC<TransitionProps> = (props) => {
const { children, classNames, animation, wrapper, ...restProps } = props;
const nodeRef = React.useRef(null);
return wrapper ? (
<CSSTransition
nodeRef={nodeRef}
classNames={classNames ? classNames : animation}
{...restProps}
>
{<div ref={nodeRef}>{children}</div>}
</CSSTransition>
) : (
<CSSTransition
classNames={classNames ? classNames : animation}
{...restProps}
>
{children}
</CSSTransition>
);
这样子在使用wrapper时没问题,但如何取消使用wrapper时的报警呢?