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

122
Visualizações
Input field value is not updating in React form

I am creating some input fields dynamically in react but once I update the value, it doesn't update in the input field and show the previous value.

const Content = input => {
    const [inputList, setInputList] = useState({}); // it is initialized in other function with some dynamic keys:

        const getFiltersValues = filters => {
              let data = {};
              for (let i = 0; i < filters.length; i++) {
                  data[filters[i]] = [];
              }
              // ... some other processing which stores the data in data object against each key
              let list = {};
              for (const [key, values] of Object.entries(data)) {  
                  list[`${key}a`] = 0;
                  list[`${key}b`] = 0; // key is something like 'data size'
              }
              setInputList(list);
        };
        // setting up value like this:
        const handleChange = (e, field) => {
            const list = inputList;
        list[`${field}a`] = parseInt(e.target.value);
        setInputList(list);
    };
     
     // rendering input fields:
        return (
            <>
                 {filters && filters.length > 0 && (
                                    <div>
                                        {filters.map(f => {
                                            return (
                                                <div>
                                             // correct updated value is shown on the console but its not updated in the 'value' attribute
                                                  {console.log(inputList[`${f.value}a`])}
                                                   <input
                                                   value={inputList[`${f.value}a`] || 0}
                                                   type="number"
                                                   onChange={e => handleChange(e, f.value)}
                                                 />
                                                    </div>
                                                </div>
                                            );
                                        })}
                                    </div>
                                )}
            </>
    );
    
};

Any suggestions/hints where I am getting wrong? Thank you.

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

0

It was a mistake while updating the object. It should have done in this way:

const handleChange = (e, field) => {
            const list = {...inputList};
        list[`${field}a`] = parseInt(e.target.value);
        setInputList(list);
    };

First copy the inputList to list and then update it and set again.

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