D
D
DeniSidorenko2021-01-06 08:35:04
React
DeniSidorenko, 2021-01-06 08:35:04

How to make console.log execute only after the data has been written?

hello i have this code

const [data, setData] = useState({
    email: "",
    password: "",
    passwordCheck: "",
    phone: "",
    username: "",
    address: ""
  })

const onChangeHandler = (event) => {
    setData({...data, [event.target.name]: event.target.value})
    console.log(data)
  }


<label>E-mail*</label>
            <input type="email" onChange={onChangeHandler} defaultValue={data.email} name="email"/>
            <div className="form-error"></div>


When you type in input, the data is stored in state. BUT console.log(data) shows with a delay in one action (Although it stands after setData) for example, if I write in input Test , then console.log will show that only Tes
is in state And why such an error or where you can only console.log after updating state. Important for validation

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Shvedov, 2021-01-06
@DeniSidorenko

In useEffect

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question