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

80
Visualizações
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 Respostas
Responde à pergunta

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 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