<div id='root' ></div>
*,
*::before,
*::after {
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background-color: #1a1d1d;
padding: 20px;
font-size: 0.875rem;
}
button {
background-color: #4caf50;
color: white;
padding: 10px 15px;
margin: 5px 10px;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
max-width: 30rem;
margin: auto;
}
.error {
color: #e73e31;
}
.equation-container {
display: grid;
grid-template-columns: auto auto auto;
align-items: center;
gap: 12px;
}
.equation {
display: flex;
flex-direction: column;
align-items: flex-end;
border: solid 1px #ddd;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
min-width: 130px;
}
.equation p {
margin: 0;
}
.equation-line {
height: 1px;
width: 80%;
margin-top: 5px;
margin-bottom: 5px;
background-color: #000;
}
.buttons {
display: flex;
flex-direction: column;
}
.header {
font-family: "Roboto", sans-serif;
font-weight: 400;
color: #4caf50;
background-color: #f5f5f5;
padding: 12px 12px;
text-align: center;
width: 100%;
&.error {
color: #e73e31;
}
}
.equation-body {
width: 50%;
display: flex;
padding: 12px 0;
flex-direction: column;
align-items: flex-end;
align-self: center;
}
.equation-body * {
padding-right: 0.5rem;
}
@media screen and (max-width: 768px) {
body {
padding: 5px;
}
.equation-container {
grid-template-columns: auto;
gap: 20px;
}
.container {
padding: 5px;
width: 100%;
}
.equation {
min-width: auto;
width: 100%;
}
.buttons {
flex-direction: row;
justify-content: center;
}
}
import React, {
useState,
useRef,
useEffect
} from "https://esm.sh/react@18.2.0";
import ReactDOM from "https://esm.sh/react-dom@18.2.0";
const App = () => {
const [addend1, setAddend1] = useState(0);
const [addend2, setAddend2] = useState(0);
const [badUseEffectSum, setBadUseEffectSum] = useState(0);
const [goodUseEffectSum, setGoodUseEffectSum] = useState(0);
const [everyRerenderSum, setEveryRerenderSum] = useState(0);
// Incorrectly omiting the dependency array
useEffect(() => {
const newSum = addend1 + addend2;
// Imagine this operation is CPU-intensive or has side-effects
console.log(
`This runs on every render, which can be a problem: Sum is ${newSum}`
);
});
// Incorrectly only including one dependency
useEffect(() => {
const newSum = addend1 + addend2;
setBadUseEffectSum(newSum);
console.log(`Sum is ${newSum}`);
}, [addend1]); // This will only re-run when `addend1` changes, not `addend2`
useEffect(() => {
const newSum = addend1 + addend2;
setGoodUseEffectSum(newSum);
console.log(`Sum is ${newSum}`);
}, [addend1, addend2]);
return (
<div className="container">
<div className="equation-container">
<div className="equation error">
<p className="header error">bad useEffect</p>
<div className="equation-body">
<p>{addend1}</p>
<p>+ {addend2}</p>
<div className="equation-line"></div>
<p>{badUseEffectSum}</p>
</div>
</div>
<div className="equation">
<p className="header">good useEffect</p>
<div className="equation-body">
<p>{addend1}</p>
<p>+ {addend2}</p>
<div className="equation-line"></div>
<p>{goodUseEffectSum}</p>
</div>
</div>
<div className="buttons">
<button onClick={() => setAddend1(addend1 + 1)}>
Increment Addend 1
</button>
<button onClick={() => setAddend2(addend2 + 1)}>
Increment Addend 2
</button>
</div>
</div>
</div>
);
};
ReactDOM.render(<App />, document.getElementById("root"));
View Compiled
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.