-2

I have the following code:

import ReactDOM from 'react-dom/client'; import { useState, useEffect } from 'react'; const App = () => { useEffect(() => { console.log('Render'); }); const [item, setItem] = useState([]); const [word, setWord] = useState([]); return ( <div> <input value={item} onChange={(e) => {setItem(e.target.value)}}></input> <label>{word}</label> <button onClick={() => setWord(new Date().toString())}>Submit</button> </div> ); } const root = ReactDOM.createRoot(document.getElementById('root')); root.render( <App /> ); 

Why is it that console.log('Render') will show the number of times it was printed by typing in the <input>, and the number of times it was printed from clicking the <button>?

Here you can see it printed 'Render' three times when I typed 'abc', and then it started a new count of 'Render' when I clicked the button twice:

Image showing number of times a log was printed

Why doesn't it just say 5 Render?

9
  • I don't understand what's the problem here.CommentedJan 23 at 3:03
  • I've updated the question.CommentedJan 23 at 3:03
  • There doesn't appear to be any reference for the message grouping behaviour so it's really just a decision made by the Chrome developers. Why is it important anyway?
    – Phil
    CommentedJan 23 at 3:06
  • @Phil I'm just curious as to why it would do that, it seems strange that it would reset the count for apparently no reason.CommentedJan 23 at 3:08
  • Probably due to the trigger being a different user event (keypress vs click). You would need to dig into the Chromium source code for a canonical answer
    – Phil
    CommentedJan 23 at 3:09

1 Answer 1

1

The console automatically separates logs that originate from different user actions, maintaining separate counters for each interaction type.

This distinction is helpful in debugging because it allows you to see how different interactions impact the component lifecycle independently.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.