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

Props are changing in value (they should be read-only)

Here is my code:

function Headline(props) {
  
  props.text = "I have changed.";
  
  return (
    <h1>{props.text}</h1>
  );
}

let title = <Headline text="Do Not Change" />;


let app = document.getElementById("app");
ReactDOM.createRoot(app).render(title);

The documentation on React website seems to imply that Props are read only (https://reactjs.org/docs/components-and-props.html#props-are-read-only). Why was I able to change its value then, because the final output for me was "I have changed.".

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

>Solution :

The documentation you link to says don’t do that not it is impossible to do that.

The changed value renders because you change it before the first render. If it had rendered and then you, later on, changed it (e.g. from a click callback) then that wouldn’t trigger a re-render.

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