search

Home  >  Q&A  >  body text

React cannot parse the value of a variable

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粉769413355P粉769413355448 days ago450

reply all(1)I'll reply

  • P粉253800312

    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])

    reply
    0
  • Cancelreply