admin 管理员组文章数量: 1086019
I have created a simple component to show my problem,
"use client";
import { useEffect, useMemo, useState, memo } from "react";
import "./App.css";
import ChildComponent from "./components/child-component";
import Child2 from "./components/child2";
function App() {
const [counter, setCounter] = useState(0);
return (
<div>
<button onClick={() => setCounter(counter + 1)}>
Counter is {counter}
</button>
<ChildComponent />
<Child2 />
</div>
);
}
export default App;
The ChildComponent
import { memo } from "react";
const ChildComponent = () => {
console.log("child renders");
return <button>Click me</button>;
};
export default memo(ChildComponent);
The Child2 component
const Child2 = () => {
console.log("child 2 rerenders");
return <div>HIii theere</div>;
};
export default Child2;
Now the problem is every time I click on the counter button I see the console log as child renders
which is from the ChildComponent
and child2 rerenders
which is from the Child2
component, which suggests to me anytime the parent component has some change it rerenders the child, how can I solve this issue.
PS - I have tried using memo and callback but haven't found a potential solution.
本文标签: javascriptIs there a way to stop rerendering the child componentsStack Overflow
版权声明:本文标题:javascript - Is there a way to stop rerendering the child components - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744041761a2523374.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论