Currently @ Bank of America
Blogging @ dsfaisal.com
Remove duplicates using Set:
const unique = [...new Set([1, 2, 2, 3, 3, 4])] // [1, 2, 3, 4]
Remove duplicates using Set:
const unique = [...new Set([1, 2, 2, 3, 3, 4])] // [1, 2, 3, 4]
Optional Chaining for Safe Property Access
const user = {
address: {
street: "123 Main St"
}
}
// Old way
const street = user && user.address && user.address.street
// Modern way
const street = user?.address?.street
Optional Chaining for Safe Property Access
const user = {
address: {
street: "123 Main St"
}
}
// Old way
const street = user && user.address && user.address.street
// Modern way
const street = user?.address?.street
Nullish Coalescing vs Logical OR
Nullish Coalescing vs Logical OR
A Stack is a linear data structure that follows LIFO (Last In First Out).
Think of it like a stack of plates - you can only:
1. Add (push) a plate to the top
2. Remove (pop) a plate from the top
3. Look at (peek) the top plate without removing it
A Stack is a linear data structure that follows LIFO (Last In First Out).
Think of it like a stack of plates - you can only:
1. Add (push) a plate to the top
2. Remove (pop) a plate from the top
3. Look at (peek) the top plate without removing it
The cn function combines two popular utilities:
1️⃣ clsx - For conditionally joining CSS class names
2️⃣ tailwind-merge - For merging Tailwind CSS classes intelligently, removing conflicts
The cn function combines two popular utilities:
1️⃣ clsx - For conditionally joining CSS class names
2️⃣ tailwind-merge - For merging Tailwind CSS classes intelligently, removing conflicts
• Non-blocking state updates
• Background processing
• Loading state management
• Concurrent rendering ready
#react
• Non-blocking state updates
• Background processing
• Loading state management
• Concurrent rendering ready
#react
• Timers: clearInterval
• Events: removeEventListener
• Subscriptions: unsubscribe
• Connections: disconnect
• API: cancel request
🚀 Why useEffect Cleanup?
No cleanup = Memory leaks
Missing cleanup = Bugs
Bad cleanup = Weird behavior
#ReactJS #javascript
• Timers: clearInterval
• Events: removeEventListener
• Subscriptions: unsubscribe
• Connections: disconnect
• API: cancel request
🚀 Why useEffect Cleanup?
No cleanup = Memory leaks
Missing cleanup = Bugs
Bad cleanup = Weird behavior
#ReactJS #javascript
useEffect:
• Runs after paint
• Async, non-blocking
• Use for: data fetching, subscriptions
• Default choice for most cases
useLayoutEffect:
• Runs before paint
• Sync, blocking
• Use for: DOM measurements, preventing flickers
• Performance sensitive
useEffect:
• Runs after paint
• Async, non-blocking
• Use for: data fetching, subscriptions
• Default choice for most cases
useLayoutEffect:
• Runs before paint
• Sync, blocking
• Use for: DOM measurements, preventing flickers
• Performance sensitive
// Run once
useEffect(() => {}, [])
// Run on changes
useEffect(() => {}, [data])
// Run & cleanup
useEffect(() => {
return () => cleanup()
}, [])
No magic, just timing!
#ReactJS #javascript
// Run once
useEffect(() => {}, [])
// Run on changes
useEffect(() => {}, [data])
// Run & cleanup
useEffect(() => {
return () => cleanup()
}, [])
No magic, just timing!
#ReactJS #javascript
const ref = useRef(initialValue)
3 Things to Remember:
• It's mutable (ref.current)
• Changes are instant
• Won't trigger re-renders
Perfect for:
📍 DOM references
⏱️ Timers
🗃️ Previous values
#React #JavaScript
const ref = useRef(initialValue)
3 Things to Remember:
• It's mutable (ref.current)
• Changes are instant
• Won't trigger re-renders
Perfect for:
📍 DOM references
⏱️ Timers
🗃️ Previous values
#React #JavaScript
const [data, setData] = useState(initialValue)
3 Rules to Remember:
1. State updates trigger re-renders
2. State updates are async
3. Previous state? Use callback:
setData(prev => prev + 1)
#react #javascript
const [data, setData] = useState(initialValue)
3 Rules to Remember:
1. State updates trigger re-renders
2. State updates are async
3. Previous state? Use callback:
setData(prev => prev + 1)
#react #javascript
It's like a toy box:
- box = your state
- toys = your data
- replacing toys = setState
When you put in new toys (setState),
React shows everyone your new toys (re-render)!
#React
It's like a toy box:
- box = your state
- toys = your data
- replacing toys = setState
When you put in new toys (setState),
React shows everyone your new toys (re-render)!
#React
useCallback vs useMemo
useCallback: Memoizes a FUNCTION
const fn = useCallback(() => doSomething(), [deps])
useMemo: Memoizes a VALUE
const value = useMemo(() => computeValue(), [deps])
#React #JavaScript
useCallback vs useMemo
useCallback: Memoizes a FUNCTION
const fn = useCallback(() => doSomething(), [deps])
useMemo: Memoizes a VALUE
const value = useMemo(() => computeValue(), [deps])
#React #JavaScript
Key difference:
Pass by Value - Function can't modify original (primitive types)
Pass by Reference - Function can modify original (reference types)
Key difference:
Pass by Value - Function can't modify original (primitive types)
Pass by Reference - Function can modify original (reference types)
Stop concatenating currency symbols! Use Intl.NumberFormat for proper formatting:
#javascript #webdev
Stop concatenating currency symbols! Use Intl.NumberFormat for proper formatting:
#javascript #webdev
Two ways to group by JavaScript with zero dependencies:
✅ using reduce()
✅ using Object.groupBy()
Two ways to group by JavaScript with zero dependencies:
✅ using reduce()
✅ using Object.groupBy()
map() is an array method in JavaScript that:
- Creates a new array
- Transforms each element of the original array
- Returns a new array of the same length
- Does NOT modify the original array
map() is an array method in JavaScript that:
- Creates a new array
- Transforms each element of the original array
- Returns a new array of the same length
- Does NOT modify the original array