This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useState } from 'react'; | |
function ComponentName() { | |
const [data, setData] = useState(null); | |
useEffect(() => { | |
// Create a subscription | |
const subscription = someDataStream.subscribe(data => { | |
setData(data); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
moment('2010-10-20').isBefore('2010-10-21'); | |
moment('2010-10-20').isBefore('2010-12-31', 'year'); // false | |
moment('2010-10-20').isSame('2010-10-20'); // true | |
moment('2010-10-20').isAfter('2010-10-19'); // true | |
moment('2010-10-20').isSameOrBefore('2010-10-21'); // true | |
moment('2010-10-20').isSameOrAfter('2010-10-21'); // false | |
moment('2010-10-20').isBetween('2010-10-19', '2010-10-25'); // true | |
moment('2010-10-20').isBetween('2010-10-19', undefined); // true, since moment(undefined) evaluates as moment() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Form() { | |
const [firstName, setFirstName] = useState('Taylor'); | |
const [lastName, setLastName] = useState('Swift'); | |
// ✅ Good: calculated during rendering | |
const fullName = firstName + ' ' + lastName; | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function TodoList({ todos, filter }) { | |
const [newTodo, setNewTodo] = useState(''); | |
// ✅ This is fine if getFilteredTodos() is not slow. | |
const visibleTodos = getFilteredTodos(todos, filter); | |
// ... | |
} | |
import { useMemo, useState } from 'react'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function ProfilePage({ userId }) { | |
return ( | |
<Profile | |
userId={userId} | |
key={userId} | |
/> | |
); | |
} | |
function Profile({ userId }) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function List({ items }) { | |
const [isReverse, setIsReverse] = useState(false); | |
const [selection, setSelection] = useState(null); | |
// Better: Adjust the state while rendering | |
const [prevItems, setPrevItems] = useState(items); | |
if (items !== prevItems) { | |
setPrevItems(items); | |
setSelection(null); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function List({ items }) { | |
const [isReverse, setIsReverse] = useState(false); | |
const [selectedId, setSelectedId] = useState(null); | |
// ✅ Best: Calculate everything during rendering | |
const selection = items.find(item => item.id === selectedId) ?? null; | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ProductPage({ product, addToCart }) { | |
// ✅ Good: Event-specific logic is called from event handlers | |
function buyProduct() { | |
addToCart(product); | |
showNotification(`Added ${product.name} to the shopping cart!`); | |
} | |
function handleBuyClick() { | |
buyProduct(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Form() { | |
const [firstName, setFirstName] = useState(''); | |
const [lastName, setLastName] = useState(''); | |
// ✅ Good: This logic runs because the component was displayed | |
useEffect(() => { | |
post('/analytics/event', { eventName: 'visit_form' }); | |
}, []); | |
function handleSubmit(e) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Game() { | |
const [card, setCard] = useState(null); | |
const [goldCardCount, setGoldCardCount] = useState(0); | |
const [round, setRound] = useState(1); | |
// ✅ Calculate what you can during rendering | |
const isGameOver = round > 5; | |
function handlePlaceCard(nextCard) { | |
if (isGameOver) { |
OlderNewer