Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda