gpt4 book ai didi

javascript - 在 StrictMode 中不推荐使用 findDOMNode 如何改用 refs

转载 作者:行者123 更新时间:2023-12-04 01:14:53 26 4
gpt4 key购买 nike

大家好,我只是想在路由器转换期间使用 Refs 来定位组件中的元素。目前,来自 React Transition Group 的 onEnter、onExit 和 addEndListener 事件已经让您可以访问正在制作动画的 DOM 节点。这在一段时间内很好,但现在你可能已经知道你得到了错误。

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]


这是我的 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” Prop 添加不同的补间。
我试图通过在 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')
);

关于javascript - 在 StrictMode 中不推荐使用 findDOMNode 如何改用 refs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63677852/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com