javascript - 在 StrictMode 中不推荐使用 findDOMNode 如何改用 refs
问题描述
大家好,我只是想在路由器转换期间使用 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;
主页.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;
我的问题是我正在尝试找到一种方法来“引用”我的“主页”和“关于”组件,这样我不仅可以为它们设置动画,还可以在可能的情况下为“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 prop 时,节点不会传递给回调函数(例如 onEnter),因为用户已经可以直接访问该节点。
所以我现在只需要一种新的方式来做这件事以供将来参考,任何见解或想法都将受到欢迎。
谢谢
解决方案
快速解决方案是:
./src/index.js 文件并将其更改为:
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
对此:
ReactDOM.render(
<App />
document.getElementById('root')
);
推荐阅读
- python-3.x - 在 Python 中打印 TXT 中最后出现的单词
- sql-server - SSIS 包步骤中的此 SQL Server OUTPUT 子句效果是什么?
- php - 存储外观获取方法返回文件存在时在路径中找不到文件 - Laravel 5.5
- node.js - Angular - 找不到模块(fs)
- python - 使用 tqdm 跟踪慢速 REST 请求的进度:填充列表的进度
- java - Inheritance.JOINED,插入导致不必要的更新
- rest - 简单 POST 请求的错误请求
- azure-resource-manager - 如何导出包含其关联 ISE 的逻辑应用的 ARM 模板?
- python-3.x - 如何从 Google BigQuery 行迭代器对象返回字符串?
- javascript - 使用 JS 执行器向 xpath 元素输入文本