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