You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Create a counter component with increment and decrement functions. Pass these functions to a child component which has buttons to perform the increment and decrement actions. Use useCallback to ensure that these functions are not recreated on every render.import{useCallback,useState,memo}from'react';exportfunctionAssignment1(){const[count,setCount]=useState(0);// Your code starts hereconsthandleIncrement=useCallback(()=>{setCount((count)=>count+1);},[]);consthandleDecrement=useCallback(()=>{setCount((count)=>count-1);},[count]);// Your code ends herereturn(<div><p>Count: {count}</p><CounterButtonsonIncrement={handleIncrement}onDecrement={handleDecrement}/></div>);}constCounterButtons=memo(({ onIncrement, onDecrement })=>(<div><buttononClick={onIncrement}>Increment</button><buttononClick={onDecrement}>Decrement</button></div>));
// Create a component with a text input field and a button. The goal is to display an alert with the text entered when the button is clicked. Use useCallback to memoize the event handler function that triggers the alert, ensuring it's not recreated on every render.// Currently we only have inputText as a state variable and hence you might not see the benefits of// useCallback. We're also not passing it down to another component as a prop which is another reason for you to not see it's benefits immediately.import{useState,useCallback}from'react';importPropTypesfrom'prop-types';exportfunctionAssignment2(){const[inputText,setInputText]=useState('');// Your code starts hereconstshowAlertCallback=useCallback(()=>{alert(inputText);},[inputText]);// Your code ends herereturn(<div><inputtype='text'value={inputText}onChange={(e)=>setInputText(e.target.value)}placeholder='Enter some text'/><AlertshowAlert={showAlertCallback}/></div>);}functionAlert({ showAlert }){return<buttononClick={showAlert}>Show Alert</button>;}Alert.propTypes={showAlert: PropTypes.func.isRequired,};