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

200
Visualizações
Are react recoil state objects silently immutable?

Am using react recoil for my state management and have a moderately nested object serving as state for this particular component.

When trying to update the state with setState (the Recoil version, I'm suspecting vanilla React useState would behave similar), I grab the old version of the object, and try mutating it a bit before returning the updated state. But reassigning values on this object fails completely silently, I see no warnings, but assigning a new value has no effect. Example:

import { useRecoilState} from "recoil";

...

const [state, setState] = useRecoilState(sampleState);

const someCallback = () => {
        setState((oldState) => {
            const newState = { ...oldState };
            console.log(newState.dog.paw); // false
            newState.dog.paw = true

            //stopping the debugger here and trying to reassign in the devtools has equally no effect

            console.log(newState.dog.paw); // false
            return newState ;
        });
    };

when reassigning during a paused debugger session in chrome dev tools this is happening:

newState.dog.paw = true
true
newState.dog.paw
false

I am aware that React's documentation says that mutating the state object directly is not advisable but from that to explaining what is happening above is a gap I don't know how to fill.

I actually also know how to work around the problem, the solution is to spread reassign the parent object like so:

newState.dog = {...newState.dog, paw:true}

Having said all that, I'd be grateful for some deeper insight about what is happening up there and why is this phenomenon behaving so weirdly in the interpreter/devtools -> the reassignment seems to go through but it doesn't have any effects.

about 4 years ago · Juan Pablo Isaza
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