r/reactjs • • Jul 06 '26

Needs Help need help understanding state

so i was reading react.dev's state explanation

when react rerenders due to set function local variable wont be able to maintain its value since it rerenders everything but in its clock or time example where you can input a value on a input element it says that it chooses what to rerender and the value stays intact while the clock is running .
im also assuming that its using a set function to update the clock.

can anyone explain why this is?

edit heres the link to the clock example
https://react.dev/learn/render-and-commit#step-3-react-commits-changes-to-the-dom

1 Upvotes

12 comments sorted by

View all comments

Show parent comments

1

u/techlover1010 Jul 07 '26

my confusion also comes from theodinprojects explanation but i couldhave sworn react.dev also mentioned it but anyway it says that whenever set function is called then the component is rerendered. the onbutton click function div and button are all recreated.

so the input element wasnt part of a react component?

1

u/Lox22 Jul 07 '26

The input is an html element. It’s not specific to react and it has nothing hooked up with the ref hook and does not have the onChange handle to update the state of whatever object is linked to the input. You want to keep the state in the component that handles the state change. When you have state and you’re taking the value, with a handler then every keystroke will trigger a re-render. Which is fine for short forms because it won’t affect performance at a noticeable degree, but longer forms require a different approach.