When the input is onChange, I want the value of the variable to change and be written to the console. But when changed it writes undefined to the console. Can you help me please ?
import './App.css';
import React,{useState,useEffect} from 'react';
function App() {
let [message,setMessage] = useState("");
useEffect((message) => {
console.log(message);
},[message])
return(
<div>
<input type="text" onChange={e => setMessage(e.target.value)} />
</div>
)}
export default App;
2
Answers
The issue is, that you wrote
but it should be like this
It is because you declare
message
to useEffect as an argument in the callback function and that is a different message than the state, it is local to the useEffect.You need to write