scrimba
Advanced React
Performance
useMemo() 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

useMemo() practice
AboutCommentsNotes
useMemo() 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"

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)
}

/**
* Challenge: we've measured a performance issue with our Product
* components, and we realized we don't need to rerender them every
* time the count changes! We need your help to fix this issue!
*/

const productStyles = {
backgroundColor: darkMode ? "#2b283a" : "whitesmoke",
color: darkMode ? "white" : "#2b283a"
}

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} style={productStyles} />
))
}
</div>
</>
)
}

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