Answers for "usememo react js"

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
4

usememo hook react

const [a,setA]=useState(0);
const [b,setB]=useState(0);

const pow=(a)=>{
  return Math.pow(a,2);
}

var val= useMemo(()=>{
  return pow(a);  // calling pow function using useMemo hook
},[a]); // only will be called once a will changed (for "a" we can maintain state)


return(
  
   <input type="text" onChange={(e)=>{setA(e.target.value)}}>
   <input type="text" onChange={(e)=>{setB(e.target.value)}}>
  
  

  {val} // to access the value from useMemo
)
Posted by: Guest on April-21-2021
0

useMemo

/*
Pass a “create” function and an array of dependencies. 
useMemo will only recompute the memoized value when one 
of the dependencies has changed. This optimization helps 
to avoid expensive calculations on every render.
*/
const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);
Posted by: Guest on August-12-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language