最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a way to stop rerendering the child components - Stack Overflow

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论