That is interesting. I try to use a few states as possible myself though, of course, still use a few.
For instance it's common to see a snippet like
const [input, setInput] = useState(); const displayValue = () => { console.log(input); } return ( <> <input value={input} onChange={({target}) => setInput(target.value)} /> <button onClick={displayValue}>Display</button> </> )
Which takes the state away from the input and stores it in React. The only problem with this is it now takes process time, even though it's only a small amount, between each key stroke.
I'm much more likely to do something like
const input = useRef(null); const displayValue = () => { const { current: el } = input; console.log(el.value); } return ( <> <input ref={input} /> <button onClick={displayValue}>Display</button> </> )
This has the exact same outcome but will only need process time when the button is pressed rather than on every key stroke.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
That is interesting. I try to use a few states as possible myself though, of course, still use a few.
For instance it's common to see a snippet like
Which takes the state away from the input and stores it in React. The only problem with this is it now takes process time, even though it's only a small amount, between each key stroke.
I'm much more likely to do something like
This has the exact same outcome but will only need process time when the button is pressed rather than on every key stroke.