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

281
Visualizações
How to use controlled and uncontrolled inputs in the same react form?

I have a form in which I do not need to track the changes in real time for part of the data and only need the final data. I want to know how to submit a controlled <select> and an uncontrolled <input> in the same form.

my code for states and handlers:

    const [ethValue, setEthValue] = useState("");
    const ethValueRef = useRef();
  
    const handleBet = (e) => {
      const newEthValue = ethValueRef.current.value;
      setEthValue(newEthValue);
      e.preventDefault();
    };

    const [formData, setFormData] = React.useState({
        userTeamChosen: "",
        eventNumber: 0,
    })
  
    function handleChange(event) {
        const { name, value, type, checked } = event.target
        setFormData(prevFormData => {
            return {
                ...prevFormData,
                [name]: type === "checkbox" ? checked : value
            }
        })
    }

my forms:

            <form onSubmit={handleBet}>
                    <select
                        onChange={handleChange}
                        name="userTeamChosen"
                        value={formData.userTeamChosen}>

                        <option>--choose---</option>
                        <option value={a}>{a}</option>
                        <option value={b}>{b}</option>
                        <option value={c}>{c}</option>
                    </select>

                    <input
                        type="number"
                        defaultValue={ethValue}
                        ref={ethValueRef}
                        name="value"
                    />
                    <button>Submit</button>
            </form>

How do I submit both forms?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I would recommend not using refs in this case and instead going for a completely state based form. There were also some inconsistencies that are fixed now.

Your number input matches the name of your form and is being handled by your existing handleChange function.

        <input
          type="number"
          name="eventNumber"
          value={formData.eventNumber}
          onChange={handleChange}
        />

Additionally I've added a little debug submit function to your form:

  const submit = () => {
    alert(JSON.stringify(formData));
  };
...
      <form onSubmit={submit}>
...

Edit frosty-tesla-wl1phe

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