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

139
Views
how can I make a universal function to change the local state

I have onChangeName and onChangeAge functions, but I want to make 1 function from these two.

functions onChangeSomeState does not work, I don't no why

codesandbox

import "./styles.css";
import { useState } from "react";

export default function App() {
  const [name, setName] = useState("");
  const [age, setAge] = useState("");

  const onChangeSomeState = (setFunc) => (event) => {
    setFunc(event.taget.value);
  };

  const onChangeName = (e) => {
    setName(e.target.value);
  };

  const onChangeAge = (e) => {
    setAge(e.target.value);
  };

  const onSubmit = () => {
    const res = JSON.stringify({
      name: name,
      age: age
    });
    console.log(res);
  };

  console.log(name);
  console.log(age);

  return (
    <div className="App">
      <input
        onChange={onChangeName}
        placeholder="name"
        value={name}
        className="input"
      />
      <input
        onChange={() => onChangeSomeState(setAge())}
        // onChange={onChangeAge}
        placeholder="age"
        value={age}
        className="input"
      />
      <button onClick={onSubmit} type="button" className="button">
        submit
      </button>
    </div>
  );
}

I'm not getting any age value in the console

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can just do this:

onChange={onChangeSomeState(setAge)}

When you do onChange={() => onChangeSomeState(setAge())}, you are not passing the event to the handler function.

You can also do:

onChange={(e) => onChangeSomeState(setAge)(e)}

Also there was a typo: setFunc(event.taget.value); missing an r in target

about 4 years ago · Juan Pablo Isaza 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!