scrimba
Frontend Career Path
Advanced React
Performance
React.memo() practice
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

AboutCommentsNotes
React.memo() practice
Expand for more info
index.jsx
run
preview
console
import React from 'react';
import ReactDOM from 'react-dom/client';
import Product from "./Product"
import productsData from "./data"

/**
* Challenge: Using what you've learned about React.memo(),
* improve the performance of this app when the counter
* is changed. Since the products don't care about the `count`
* value at all, they shouldn't need to rerender when `count`
* changes.
*/

function App() {
const [count, setCount] = React.useState(0)
const [darkMode, setDarkMode] = React.useState(false)

function increment() {
setCount(prevCount => prevCount + 1)
}

function decrement() {
setCount(prevCount => prevCount - 1)
}

return (
<>
<h1>The current count is {count}</h1>
<button className="button" onClick={decrement}>-</button>
<button className="button" onClick={increment}>+</button>
<br />
<br />
<button
className="button"
onClick={() => setDarkMode(prev => !prev)}
>
{darkMode ? "Light" : "Dark"}
</button>
<br />
<br />
<div className="products-list">
{
productsData.map(product => (
<Product
key={product.id}
product={product}
darkMode={darkMode}
/>
))
}
</div>
</>
)
}

ReactDOM.createRoot(document.getElementById('root')).render(<App />);
Console
/index.html
-3:06