Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The increaseSecondState function is re-rendering the child component even when i didn't pass it as a prop to the child component

I didnt expect the child component to re-render because i didnt pass the increaseSecondState function as a prop to the child component. Because we all know after using React.memo on the child component, the only way it can re-render is if only its prop changes.

import React, { useState, useCallback } from 'react'
import Child from './Child'

function Example() {
  const [state, setState] = useState(0)
  const [count, setCount] = useState(0)

  const increaseFirstState = () => {
    setState(state + 1)
  }

  const increaseSecondState = () => {
    setCount(count + 1)
  }

  return (
    <div>
      <Child firstFunc={increaseFirstState} />
      <h1>first state - {state}</h1>
      <h1>second state - {count}</h1>

      <button onClick={increaseFirstState}>first state</button>
      <button onClick={increaseSecondState}>second state</button>
    </div>
  )
}

export default Example

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Every time Example renders, a new increaseFirstState function gets created. It may have the same text as the previous one, but it’s a different function. Since the firstFunc prop has changed, Child must rerender, even if it uses React.memo.

To prevent the rerender, not only must you use react.memo, but you must also make sure the props don’t change. You’ll do this by using useCallback to memoize your functions.

const increaseFirstState = useCallback(() => {
  setState(prev => prev + 1);
}, []);

const increaseSecondState = useCallback(() => {
  setCount(prev => prev + 1);
}, []);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading