【发布时间】:2020-12-19 23:44:13
【问题描述】:
大家好,我只是想在路由器转换期间使用 Refs 来定位组件中的元素。目前,来自 React Transition Group 的 onEnter、onExit 和 addEndListener 事件已经让您可以访问正在制作动画的 DOM 节点。有一段时间没问题,但现在你可能已经知道你得到了错误。
index.js:1 警告:在 StrictMode 中不推荐使用 findDOMNode。 findDOMNode 被传递了一个在 StrictMode 中的 Transition 实例。相反,直接将 ref 添加到要引用的元素。详细了解如何安全使用 refs [1]
这是我的 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;
关于.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;
我的问题是我正在尝试找到一种方法来“引用”我的“Home”和“About”组件,这样我不仅可以为它们设置动画,还可以为“onExit”和“onEnter”道具添加不同的补间,如果可能。
我已尝试通过在 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>
);
正如预期的那样,这会导致我的应用程序停止工作,因为“nodeRef”返回 false,并且当使用 nodeRef 属性时,节点不会传递给回调函数(例如 onEnter),因为用户已经可以直接访问节点。
所以我现在只需要一种新的方式来做这件事以供将来参考,任何见解或想法都会受到欢迎。
谢谢
【问题讨论】:
-
我在您包含的代码中的任何地方都看不到
findDOMNode。您正在使用的第三方库是否正在调用它?如果是这样,您将无能为力。 -
@Sibox React-Transition-Group 使用它来引用节点。 reactcommunity.org/react-transition-group/transition
-
如果它被 React Transition Group 调用,那么你无能为力。我不太熟悉那个包的底层机制,但是here's a discussion from their issue tracker。
-
@Slbox 非常感谢您的帮助。它让我找到了答案,请参见下文:)
标签: javascript reactjs routes react-router