console log the state after using useState doesn't return the current value
useState
by default simply does one thing and one thing only, set the new state and cause a re-render of the function. It is asynchronous in nature so by default, methods running after it usually run.
From your example, on a fresh load of the page, typing 's' causes useState to change the state, but because it is asynchronous, console.log
will be called with the old state value, i.e. undefined
(since you didn't set a value. You should consider setting an initial state, if you want to)
const [weather, setWeather] = useState(''); // Set the intial state
The only way to truly read the value of the state is to use useEffect
, which is called when there is a re-render of the component. Your method simply becomes:
import React, { useEffect, useState } from 'react';
import ReactDOM from 'react-dom';
function Weather() {
const [weather, setWeather] = useState('');
useEffect(() => console.log(weather), [weather]);
const changeValue = event => setWeather(event.target.value);
return <input value={weather} onChange={changeValue} />;
}
const rootElement = document.getElementById('root');
ReactDOM.render(<Weather />, rootElement);
When you call setWeather
, you trigger a rerender of your component, which will call again this function. However, the console.log(weather);
inside the function still references the first value returned by useState
, which is your orignal value. If you want to print it you should be doing :
console.log(e.target.value);