简体   繁体   中英

How do I console.log state with forms in React hooks?

When I submit text in the form, I want to be able to see that text in state via console.log. I added a console.log to trigger directly after state has been added, but I don't see anything in my console, what am I doing wrong?

What I would like to see: I submit "test1" then I submit "test2", I then want to see in my console "test1, test2" in state.

import React, { useState } from 'react';
import './App.css';

function App() {
    return (
      <div>
        <Field />
      </div>
    );
  }


function Field(){
  const [toDoItem, setToDoItem] = useState('');


  const addToDoItem = (event) => {
    setToDoItem(event.target.value), function(){
      console.log(toDoItem)
    }
  }

  return (
        <form>
            <input type="text" value={toDoItem} onChange={addToDoItem}/>
            <input type="submit" value="Add" />
        </form>
  );
}

export default App;

useState doesn't provide callback after setting value. You can use useEffect instead.

  React.useEffect(() => {
    console.log(toDoItem);
  }, [toDoItem]);

EDIT

It seems like that you want to get toDoItem value on submit. The problem is that the page is reloaded when form is submitted. You can use event.prefentDefault() to stop refreshing on form submission.

<form onSubmit={onSubmit}>
  const onSubmit = (event) => {
    event.preventDefault()
    console.log(toDoItem)
  }

You can log the change in states using a useEffect

I'd suggest making a helper function if you tend to do it often:

function useLog(name, property) {
  useEffect(() => console.log(name, property), [name, property]);
}

Which you'd use as follows:

useLog('toDoItem', toDoItem);

I set up an example where the toDoItem is logged as it changes and when you submit, it also logs the change in a todoItems array

 const { useState, useEffect } = React; function useLog(name, property) { useEffect(() => console.log(name, property), [name, property]); } function App() { return ( <div> <Field /> </div> ); } function Field() { const [toDoItem, setToDoItem] = useState(''); useLog('toDoItem', toDoItem); const [todos, setTodos] = useState([]); useLog('todos', todos); const changeTodo = (event) => { setToDoItem(event.target.value); }; const addTodoItem = (event) => { event.preventDefault(); setTodos((prev) => prev.concat(toDoItem)); setToDoItem(''); }; return ( <form onSubmit={addTodoItem}> <input type="text" value={toDoItem} onChange={changeTodo} /> <input type="submit" value="Add" /> </form> ); } ReactDOM.render(<App />, document.getElementById('root'));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.1/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.1/umd/react-dom.production.min.js"></script> <div id="root"/>

import React, { useState } from 'react';
import './App.css';

function App() {
    return (
      <div>
        <Field />
      </div>
    );
  }


function Field(){
  const [toDoItem, setToDoItem] = useStateWithCallback('', toDoItem => {
    console.log(toDoItem);
  });


  const addToDoItem = (event) => {
    setToDoItem(event.target.value);
  }

  return (
        <form>
            <input type="text" value={toDoItem} onChange={addToDoItem}/>
            <input type="submit" value="Add" />
        </form>
  );
}

export default App;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM