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

387
Visualizações
React: Storing static variable in state vs render variable

I found a similar discussion to my question here but wanted to dig deeper.

I have yet to find any React documentation that discusses whether static variables (that are unchanging but need to be used for a React view) should be stored as a state variable or be rebuilt each time the component is rerendered.

Let's say that I have a React component that takes in a prop, which is a list, and I want to map this list to a certain output. In my use case I am turning the list into the format for the options my component's select picker will use. Once this list is made, it does not need to change. All options will remain the same for the rest of the component's use.

It feels weird to throw it into state, although it just seems more efficient. Let's go through two approaches.

Approach #1: Store option list in component state.

class Component extends React.Component {

    constructor(props) {
       this.state = {
          options: props.list.map(some => computation)
       }
    }

    render() {
        return ( <SelectPicker options={this.state.options} /> )
    }

}

Approach #2: Recreate variable upon each rerender.

class Component extends React.Component {

    constructor(props) {
        ...
    }

    render() {
        const options = this.props.list.map(some => computation)

        return ( <SelectPicker options={options} /> )
    }

}

The latter seems more correct, in that since the options array is never meant to change, it is only meant to be initialized once, and it doesn't make sense to put a watcher on it for being part of state. The former just seems more efficient, as we only compute this value once, rather than recomputing it every single time the component is rerendered. Yes React will compare the state between rerenders, but won't it compare the options list references? Whereas the latter example will completely rebuild a new list? Tbh, neither of these seem like the "clean" approaches to this.

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

The better option is neither. In your constructor function, set this.options to the mapped array and then access it with this.options in your render method.

class Component extends React.Component {

    constructor(props) {
       this.options = props.list.map(some => computation);
    }

    render() {
        return ( <SelectPicker options={this.options} /> )
    }

}

This avoids creating the same array over and over again. It's perfectly fine to set values on this. directly, the point of state is that if a state variable changes the render method is called again.

about 4 years ago · Santiago Gelvez 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