Answers for "react hooks ref vs state"

32

useRef

/*
	A common use case is to access a child imperatively: 
*/

function TextInputWithFocusButton() {
  const inputEl = useRef(null);
  const onButtonClick = () => {
    // `current` points to the mounted text input element
    inputEl.current.focus();
  };
  return (
    <>
      <input ref={inputEl} type="text" />
      <button onClick={onButtonClick}>Focus the input</button>
    </>
  );
}
Posted by: Guest on March-29-2020
0

use state vs use ref

Source: https://stackoverflow.com/a/56456055
The main difference between both is : useState causes re-render, useRef does not.
Posted by: Guest on March-04-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language