Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

335
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda