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

164
Visualizações
ReactJS + Typescript: Set component state with context property value without triggering a rerender

I am relatively new to ReactJS and I'm building an app with a Context. At some point I need to get one of the values from the Context, make it available for editing and only after it's submitted change it's value in the Context (to avoid refreshing all the other components). I thought of doing something like this:

export default class AComponent extends Component<any, { property?: Property }> {
  public constructor(props: any) {
    super(props);
  }

  public shouldComponentUpdate(nextProps: any, nextState: { property?: Property }) {
    return nextState.property !== this.state.property;
  }

  public render(): JSX.Element {
    return (
      <AContext.Consumer>
        {(data) => {
          // ...
          this.setState({ ...this.state, property: data.property });
          // ...    
        }}
      </AContext.Consumer>
    );
  }
}

But even with the shouldComponentUpdate check, it still refreshes 4 times! Without it it produces an error (recursion limit reached or something like that).

The question is: What's the correct way of working with a context property without changing it until the moment you actually want to?

I wanted to get the Context property inside the constructor but I have found that this form is deprecated:

constructor(props: any, context: AContext) {
  super(props, context)
}

Is there any other way?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

not only can property add to context but you can also add methods to them.

  • Context is designed to share data that can be considered “global” for a tree of React components, such as the current authenticated user, theme, or preferred language

  • if you want to change data from different components, I prefer to use state management like Redux

    happy codeing

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