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

81
Vistas
Validating form inputs with React

I am trying to check that the required fields are not empty and making sure that the input type is correct.

const CreateSensor = () => {
    const [deveui, setDeveui] = useState('');
    const [location, setLocation] = useState('');
    const [levelid, setLevel] = useState('');

const submitValue = () => {

    let data = {deveui,location,levelid};
    //POST method
    fetch("api")
          
    ClearFields();      
}
function ClearFields(){
    document.getElementById("dev").value = "";
    document.getElementById("location").value = "";
    document.getElementById("level").value = "";
}

return(
    <>
    <hr/>
    <input type="text" id="dev" placeholder="deveui" onChange={e => setDeveui(e.target.value)} />
    <input type="text" id="location"placeholder="Location" onChange={e => setLocation(e.target.value)} />
    <input type="text" id="level" placeholder="Levelid" onChange={e => setLevel(e.target.value)} /> 
    <button onClick={submitValue}>Submit</button>
    </>
    )
}

the submit button will check whether deveui is not empty and the levelid is set to an integer. I have tried changing the input type for levelid to numbers but there is arrows on it which I feel is unnecessary.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I strongly recommend using a React form library. Here's an example with react-hook-form

import { useForm } from "react-hook-form";

const CreateSensor = () => {
  const {
    register,
    handleSubmit,
    watch,
    reset,
    formState: { errors },
  } = useForm({ defaultValues: { deveui: "", location: "", levelid: "" } });

  const submitValue = ({deveui, location, levelid}) => {    
    // exclude 'deveui' from fetch payload
    const payload = { location, levelid }

    // POST data to api, for example
    fetch("https://myapi.com", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(payload),
      //   reset form state
    }).then((response) => reset());
  };

  return (
    <>
      <hr />
      <form onSubmit={handleSubmit(submitValue)}>
        <input
          {...register("deveui", { required: true })}
          type="text"
          id="dev"
          placeholder="deveui"
        />
        <input
          {...register("location", { required: true })}
          type="text"
          id="location"
          placeholder="Location"
        />
        <input
          {...register("levelid", { required: true })}
          type="text"
          id="level"
          placeholder="Levelid"
        />
        <button type="submit">Submit</button>
      </form>
    </>
  );
}


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