When the onChange event occurs on the input, I want the value of the variable to change and be written to the console. But when changes occur, it writes undefined in the console. can you help me?
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;
Why does undefined appear every time onchange occurs?
P粉2538003122023-09-11 00:40:05
The problem is, you wrote
useEffect((message) => { console.log(message); },[message])
But it should be written like this
useEffect(() => { console.log(message); },[message])