Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!