Hi all Im just trying to get my head around using Refs to target elements in my components during router transitions. currently The onEnter, onExit and addEndListener events from React Transition Group already give you access to the DOM node you're animating. Which was fine for a while but now as you may already know you get the error.
index.js:1 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 [1]
here is my code for App.js
import React from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import { CSSTransition } from 'react-transition-group';
import { gsap } from 'gsap';
import Header from './tools/header';
import About from './pages/about';
import Home from './pages/home';
const routes = [
{ path: '/', name: 'Home', Component: Home },
{ path: '/about', name: 'About', Component: About },
]
function Routeapp() {
const nodeRef = React.useRef(null);
const onEnter = (node) => {
console.log(node)
gsap.from(
[node.children[0].firstElementChild, node.children[0].lastElementChild],
{
duration: 0.6,
y: 30,
delay: 0.6,
ease: 'power3.inOut',
opacity: 0,
stagger: {
amount: 0.3,
},
}
);
};
const onExit = node => {
gsap.to(
[node.children[0].firstElementChild, node.children[0].lastElementChild],
{
duration: 0.6,
y: -30,
ease: 'power3.inOut',
opacity: 0,
stagger: {
amount: 0.15,
},
}
);
};
return (
<Router>
<Header />
<div className="container">
{routes.map(({ path, Component }) => (
<Route key={path} exact path={path}>
{({ match }) => (
<CSSTransition
in={match != null}
key={path}
timeout={1200}
classNames="page"
onEnter={onEnter}
onExit={onExit}
unmountOnExit={true}
>
<div className="page" ref={nodeRef} >
<Component />
</div>
</CSSTransition>
)}
</Route>
))}
</div>
</Router>
);
}
export default Routeapp;
Home.js
import React from 'react';
import Title from '../tools/title';
const Home = () => {
return (
<div className="inner">
<Title lineContent="this is the" lineContent2="Home page" />
<div className={'infoBox'}>
<p className="info">hello mel and welcome</p>
</div>
</div>
);
};
export default Home;
about.js
import React from 'react';
import Title from '../tools/title';
const About = () => {
return (
<div className="inner">
<Title lineContent={'this is the'} lineContent2={'About page'} />
<div className={'infoBox'}>
<p className="info pink">
Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and
scrambled it to make a type specimen book. It has survived not only
five centuries, but also the leap into electronic typesetting,
remaining essentially unchanged. It was popularised in the 1960s with
the release of Letraset sheets containing Lorem Ipsum passages, and
more recently with desktop publishing software like Aldus PageMaker
including versions of Lorem Ipsum.
</p>
</div>
</div>
);
};
export default About;
My issue is that I am trying to find a way to 'Ref' my 'Home' and 'About' components so that I can not only animate them but possible add different tweens to the 'onExit' and 'onEnter props if possible.
I have tried to 'Ref' the components directly by trying this in app.js:
return (
<Router>
<Header />
<div className="container">
{routes.map(({ path, Component }) => (
<Route key={path} exact path={path}>
{({ match }) => (
<CSSTransition
in={match != null}
key={path}
nodeRef={nodeRef}
timeout={1200}
classNames="page"
onEnter={onEnter}
onExit={onExit}
unmountOnExit={true}
>
<div className="page" ref={nodeRef} >
<Component />
</div>
</CSSTransition>
)}
</Route>
))}
</div>
</Router>
);
as expected this causes my application to stop working since 'nodeRef' returns false and When nodeRef prop is used, node is not passed to callback functions (e.g. onEnter) because user already has direct access to the node.
So I just need a new way of doing this now for future reference, any insights or ideas will be most welcomed.
thanks
findDOMNode
anywhere in the code you included. Is it being called by a third party library you're using? If so, there's nothing you can reasonably do. – Slbox