Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
{expression}Wrap any JavaScript expression in curly braces to embed it in JSX
Connect your component state directly to the UI using expressions
Use ternary operators and logical operators for dynamic rendering
Perform mathematical operations and string manipulations inline
{if (condition) { return "Yes"; }}This will cause an error because `if` is a statement, not an expression.
{condition ? "Yes" : "No"}Use ternary operators or logical operators for conditional rendering.
const greeting = `Hello, ${name}! Today is ${day}`;Use template literals for dynamic string construction with embedded expressions
{items.map(item => <li key={item.id}>{item.name}</li>)}Transform arrays directly in JSX using map, filter, and reduce methods
const {name, age} = user;Extract properties from objects for cleaner JSX expressions
{user && <Welcome user={user} />}Use logical operators for conditional rendering without ternary operators
Use useMemo to cache expensive calculations
const expensive = useMemo(() => {
return complexCalculation(data);
}, [data]);Define functions outside render when possible
// Good: Define outside
const handleClick = () => {...};
// Avoid: Inline in JSX
<button onClick={() => {...}>Prevent unnecessary re-renders with memoized callbacks
const memoizedCallback = useCallback(
() => { doSomething(a, b); },
[a, b]
);Defer expensive operations until needed
const heavyComputation = useMemo(() =>
expensiveOperation(data), [showResults]
);<span>
${product.price.toFixed(2)}
{product.onSale && (
<span className="sale-badge">
{Math.round((1 - product.salePrice / product.price) * 100)}% OFF
</span>
)}
</span><div className="avatar">
{user.avatar ? (
<img src={user.avatar} alt={user.name} />
) : (
<div className="avatar-placeholder">
{user.name.split(' ').map(n => n[0]).join('').toUpperCase()}
</div>
)}
</div>// Assuming completed and total are defined
const completed = 75;
const total = 100;
<div className="progress-bar">
<div
className="progress-fill"
style={{ width: (completed / total) * 100 + "%" }}
>
{Math.round((completed / total) * 100)}%
</div>
</div>console.log() to inspect expression values