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

140
Visualizações
Should I use state or call a function for better performance?

I have a Redux Form page with some fields that have to be disabled when a condition is met.

My question is whether I should check this condition once when I do the fetchData call on mounting the component or if I should have some kind of function isDisabled() that will be called in the render (note: this function used the data fetched in the didMount that is already in the state).

I want to know which one (if any) would be more performant or is the better way to handle this in React.

I'll try to illustrate the two options.

Option 1: set in state on mount

componentDidMount() {
   fetch(...).then(result => {
      setState({ data: result.data, isDisabled: result.data.fixed === 1 }); 
   });
}

render() {
   const { isDisabled } = this.state;
   return (
      <Form>
        ...
        <Field disabled={isDisabled} />
      </Form>
   );
}

Option 2: check if it is disabled with a function, preventing using state twice for same data

componentDidMount() {
   fetch(...).then(result => {
      setState({ data: result.data }); 
   });
}

isDisabled() {
   const { data } = this.state;
   return data.fixed === 1;
}

render() {
   return (
      <Form>
        ...
        <Field disabled={this.isDisabled()} />
      </Form>
   );
}

Thank you for your help

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