const [text, setText] = useState('');
useEffect(() => async () => {
try {
const response = await axios.post(
'http://localhost:3001/changed',
{ text: text },
{ withCredentials: true }
);
console.log(response.data.text);
}
catch (e) {
console.log(e);
}
}, [text]);
I also have a text input like this:
<input type="text" onChange={ (e) => setText(e.target.value) } />
The backend is returning the same posted object. The problem is that when I input the first character the useEffect
logs an empty string and when I input the second character the useEffect
logs the first character only.
When I input the third character in the input field the useEffect
logs the first two characters. It is lagging behind by one character. Why this is happening?