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

Will prop values be used in `useEffect` trigger a re-render if they change?

I’d like to use a prop value from my component to retrieve a value from redux.

Will changes to that prop value trigger effects and a re-render?

For example, in the code below, would a change to the organizationId prop from a calling component result in the console message being logged with the new name?

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

import React, { useEffect } from "react";
import { useSelector } from "react-redux";

function MyComponent({ organizationId }) { 

  const organizationName = useSelector(state => 
    state.organizations[organizationId].name
  );
  
  useEffect(() => {
    console.log(`organizationName changed to ${organizationName}`);
  }, [organizationName]);

  return (
    <>{organizationName}</>
  );
}

>Solution :

Yes it will.
The effect will be called because the organizationName is listed as a dependency.

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