Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should all React component props always be readonly in Typescript?

Tags:

typescript

It seems like they should be since props should never be changed within the component. I can't find any answers on Google or Stack Overflow so asking here just to be sure.

like image 390
Jake Avatar asked Oct 19 '25 16:10

Jake


1 Answers

It's not a Typescript specificity but a React one.

React is design to render component according a set of props. If you want to change the way a component is render the parent need to generate new props and give it to the children.

The React data flow is a one-way flow from parents to children. You can't and you don't want to mutate a props coming from a parents. Because if props can be mutated it should produce UI issues with React component life cycle. A component re-render only if the reference of one of his props change. Mutate a props will not re-render component and your UI will be different from your data.

like image 84
Guillaume LEROY Avatar answered Oct 22 '25 08:10

Guillaume LEROY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!