usememo hook example
Example 1: useRef
function TextInputWithFocusButton() {
const inputEl = useRef(null);
const onButtonClick = () => {
inputEl.current.focus();
};
return (
<>
<input ref={inputEl} type="text" />
<button onClick={onButtonClick}>Focus the input</button>
</>
);
}
Example 2: 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);
},[a]);
return(
<input type="text" onChange={(e)=>{setA(e.target.value)}}>
<input type="text" onChange={(e)=>{setB(e.target.value)}}>
{val}
)