Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

334
Views
React: Why are props not frozen?

React suggests not to mutate props. I was operating under the impression that when props are passed in, they would be immutable or frozen.

The code below does not throw any errors and mutates the prop. Is this a bug?

try{
  var a = this.props.a;
  a.push('one')
  a.push('two')
  a.push('three')
  console.log(a)
}catch(err){
  console.log(err)
}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

I'm not sure of the exact reasoning behind it or if that's documented somewhere but I could take a guess:

Freezing any assignment to a variable is not easy or even necessarily possible outside of using new-ish or even as of yet unimplemented browser features. Also deep freezing all your props in your app could have performance implications and would certainly need to be removed for prod builds.

Can you freeze assignment of a primitive variable? I don't think so. I think babel does it with const by statically checking code at compile time.

And can you freeze objects without Object.freeze or ES7's proxy? Again I don't think so, the es5-shim readme says:

which you cannot possibly obtain in legacy engines

What if someone does mutate something? These props are shallow copied, meaning you won't affect the parent prop unless you modify a nested object. However even if you did it would't affect the app, as that would get wiped out on the next render (just like your modifications).

If I got something wrong please let me know! This is just my reasoning.

over 4 years ago · Santiago Trujillo Report

0

Seems like this is the same behavior as const has, you can't reassign the value, but you can still push to the array or add keys to an object because those are not protected. Anyway, despite the possibility to do those mutations, you should not do this.

this.props.a = [1,2,3]; won't work

this.props.a.push(123); will work.

this.props.a = {name: 'John Doe'} won't work

this.props.a.name = 'John Doe' will work

If you try to reassign a prop in your component a type error will occur.

Uncaught TypeError: Cannot assign to read only property a ...

over 4 years ago · Santiago Trujillo Report

0

It won't throw any error unless you frozen the object(props) explicitly.

Props are received from parents and immutable as far as the Component receiving them is concerned.

i.e React by default the props are immutable in the sense it is the concern of your component to maintain them as data received from parent and use as it is. Where as we can use state to maintain the local(component level data) that can be changed any time and keeping props contract with the parent.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!