CounterA and CounterBuseState to manage separate counts for each counterCounterB with React.memo to prevent unnecessary re-rendersCounterA and CounterBuseState to manage separate counts for each counterCounterB with React.memo to prevent unnecessary re-rendersApp. Inside it, use useState to create two state variables: countA and countB, both starting at 0.Use const [countA, setCountA] = useState(0) and similarly for countB.
CounterA and CounterB. Each should accept count and onIncrement props. Render the count inside a <div> and a button that calls onIncrement when clicked.Return a <div> with a <p> showing the count and a button that calls onIncrement.
CounterB component with React.memo to prevent it from re-rendering when its props do not change.Use const CounterB = React.memo(function CounterB(props) { ... }).
App component, render CounterA and CounterB. Pass countA and setCountA as count and onIncrement props to CounterA. Pass countB and setCountB similarly to CounterB. For onIncrement, pass a function that increases the count by 1.Render both counters and pass increment functions that add 1 to the current count.
What is the main purpose of React.memo in React?
Which of the following is the correct syntax to wrap a functional component MyComponent with React.memo?
Consider this code snippet:
const MyComponent = React.memo(({ count }) => {
console.log('Rendered');
return <div>Count: {count}</div>;
});
function App() {
const [count, setCount] = React.useState(0);
const [text, setText] = React.useState('');
return (
<>
<MyComponent count={count} />
<input value={text} onChange={e => setText(e.target.value)} />
</>
);
}What will be logged to the console when the user types in the input box?
count as prop.text state, causing App to re-render. However, the count prop remains unchanged (same primitive value), so React.memo skips re-rendering MyComponent.count does not change, React.memo prevents re-render, so 'Rendered' logs only once on mount.Consider this usage of React.memo:
const MyComponent = React.memo(({ value }) => {
return <div>Value: {value}</div>;
}, (prevProps, nextProps) => {
return prevProps.value === nextProps.value;
});Which of the following correctly describes this code?
prevProps.value === nextProps.value, which is correct to skip re-render when props are equal.You have a component wrapped with React.memo that receives a prop data which is an object. You notice it re-renders every time the parent renders, even if data has the same content. How can you fix this?
data -> Option BPass the test and this pattern moves unknown