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

97
Visualizações
check if new reference has been created in redux selector

I have the problem where I receive in my componentDidUpdate(prevProps, prevState) a new reference of the object in the incoming this.props, so even the values are equal, I get misleading object comparisons when I compare this.props with the prevProps object. To track this, I'd like to know why this happens and I think is because of Deriving Data with Selectors.

So, consider the typicall scenario

class FooComponent extends Component {
    constructor(props) {
        super(props)
    }
    ....
}

const mapStateToProps = (state, props) => ({
    stateSlice: getStateSlice(state)
}

const mapDispatchToProps = (dispatch) => ({
    ...
})

export default connect(mapStateToProps, mapDispatchToProps)(FooComponent))

For the scenario of a complex app, big global state and complex getStateSlice function, I'd like to know if a new reference has been created.

I tried to compare getStateSlice(state) with state.stateSlice in the mapStateToProps with a refCheck function like this:

function refCheck(state, props) {
    if (props.stateSlice === getStateSlice(state)) {
        console.log('sameRef!!');
    }
    return getStateSlice(state);
}

const mapStateToProps = (state, props) => ({
    stateSlice: refCheck(state)
}

And everything works, but I keep getting the same value props in the ComponentDidUpdate() and a different reference. Prof if this is that I obtain in the componentDidUpdate(prevProps, prevState)

prevProps === this.props: false
JSON.stringify(prevProps) === JSON.stringify(this.props): true

How may I check if a new reference is created to evaluate if a selector works correctly and does not create new ref at the state info retrieve step for the same values by mistake? It also'd be helpful to be able to check that at the reducer level and log whether the reducer is creating a new refernce or not.

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