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

219
Visualizações
Form data doesn't fill in React

I have a very basic project with next js in this project i have a simple form . when I submit this form i put my data in form Data with append method and after that when i log my form data in console it is an empty object and i'm so wondering why this is happened this is my code

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

export default function Home() {
    const [inputs, setInputs] = useState({
        name: '',
        mobile: ''
    });
    const changeHandler = (e) => {
        setInputs(prev => {
            const cloneState = {...prev};
            cloneState[e.target.name] = e.target.value;
            return cloneState;
        });
    }

    const clickHandler = (e) => {
        e.preventDefault();
        const formData = new FormData();
        formData.append('name', inputs.name);
        formData.append('mobile', inputs.mobile);
        console.log(formData, 'FD');
    }
    return (
        <div className={styles.container}>
            <form>
                <input type="text" onChange={changeHandler} name='name'/>
                <input type="text" onChange={changeHandler} name='mobile'/>
                <button type='submit' onClick={clickHandler}>Submit</button>
            </form>
        </div>
    )
}

thanks all of you in advanced

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

0

If you log the formData object in console it will log the formData object. You need to call the 'get' method to get the values. If you want to get a json from a formData object use the code below.

console.log(Object.fromEntries(formData))
about 4 years ago · Juan Pablo Isaza Relatório

0

The type of the instance of FormData is not a plain JS object, but its a FormData object, hence you can't log it directly into console.

To check its entries, you can do:

[...formData.entries()].forEach(e => console.log(e))
about 4 years ago · Juan Pablo Isaza Relatório

0

The problem related to getting values from formData. You can get data from formData using get() method, or getAll() in order to get all values with the same name inside the array. For getting all values from formData you can use for...of loop like this one.

for (let value of formData.values()) {
        console.log(value);
    }

Hope, this will help you. Thanks)

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