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, storing user inputs as a new object in an empty array

I'm trying to store in an empty array a user input and a unique id as an object.

So I have:

const [weight, setWeight] = useState([]);

that I want to fill with this function:

const submitWeight = () => {
    setWeight([{ value: inputWeight, id: uuidv4() }, ...weight]); // this one.
    setDefaultMessageWeight("");
    closedClass();
  };

Any ideas? Thank you!

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

0

Here is an example of how you can access a rendered input element using ref inside your component.

import { useRef, useState } from "react";

export default function App() {
  const [weight, setWeight] = useState([]);
  const inputRef = useRef(null);

  const submitWeight = () => {
    let inputWeight = inputRef.current.value;
    setWeight([{ value: inputWeight, id: uuidv4() }, ...weight]); 
    // ... 
  };

  return (
    <div>
      <input ref={inputRef} />

      <button onClick={submitWeight}>Submit</button>

      {weight.map(({ value }, index) => (
        <p>
          Weight #{index} is {value}
        </p>
      ))}
    </div>
  );
}
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