Implementing React 19 Best Practices: A Complete Migration Guide
A comprehensive walkthrough of implementing React 19 best practices in a real-world portfolio application, including enhanced ref callbacks, the new use() hook, simplified Context providers, and native metadata support.
React 19 represents a significant evolution in React development, introducing powerful new features while maintaining backward compatibility. As a technology leader who values staying current with modern development practices, I recently undertook a comprehensive migration of my portfolio application to implement React 19 best practices. This journey revealed fascinating insights about performance optimization, cleaner code patterns, and the future of React development.
Current implementation (July 2026): The portfolio now runs React 19.2.7. One important correction to the original article: automatic memoization comes from the optional React Compiler, not from the React 19 runtime alone. This site does not currently enable the compiler, so it keeps purposeful
useMemoanduseCallbackcalls where stable identity or measured render behavior matters. The blog reader currently uses cached lazy Markdown imports with an effect-based loader and retainsreact-helmet-asyncas a compatibility layer, so theuse()and native metadata examples below are demonstrations of React 19 capabilities rather than a verbatim snapshot of every current component.
Why React 19 Matters
Before diving into the implementation details, it's worth understanding why React 19 is such a significant release:
- Compiler Path: React 19 works with the optional React Compiler, which can reduce manual memoization when explicitly configured
- Enhanced Developer Experience: New hooks and patterns make common tasks simpler and more intuitive
- Better Performance: Improved rendering algorithms and automatic batching provide performance gains
- Future-Ready: New features like the
use()hook and enhanced ref callbacks prepare applications for upcoming React patterns - Simplified APIs: Cleaner syntax for common patterns like Context providers and metadata management
Assessment: Current State vs. React 19 Opportunities
My portfolio was already built with React 18 and followed many best practices:
- Functional components with hooks
- TypeScript for type safety
- Proper code splitting and lazy loading
- Modern build setup with Vite
However, I identified several opportunities for React 19 enhancement:
Areas for Improvement
- Manual Memoization: Review
useCallbackanduseMemousage, removing only calls that do not protect measured work or stable dependencies - Legacy Context Patterns: Using
.Providersyntax that can be simplified - Async Data Loading: Identify Suspense-compatible operations that are good candidates for the new
use()hook - Ref Management: Basic ref usage without proper cleanup functions
- Metadata Handling: Relying on third-party libraries for document metadata
Implementation Strategy
I approached this migration systematically, focusing on one React 19 feature at a time:
React 19 Migration Plan
├── Enhanced Ref Callbacks (Memory Management)
├── use() Hook Implementation (Async Data)
├── Simplified Context Providers (DX Improvement)
├── Native Metadata Support (SEO Enhancement)
├── Performance Optimizations (Automatic)
└── Modern TypeScript Configuration
Feature 1: Enhanced Ref Callbacks with Cleanup
One of React 19's most powerful features is enhanced ref callbacks that can return cleanup functions. This provides better memory management and eliminates common memory leaks.
Before: Basic Ref Usage
// Legacy approach - no cleanup
const BackToTop = () => {
const scrollButtonRef = useRef<HTMLButtonElement>(null);
useEffect(() => {
const element = scrollButtonRef.current;
if (element) {
element.setAttribute('data-component', 'back-to-top');
// No cleanup - potential memory leak
}
}, []);
return <button ref={scrollButtonRef}>Back to Top</button>;
};
After: Enhanced Ref Callbacks
// React 19 enhanced ref callbacks with cleanup
const BackToTop = () => {
// React 19 enhanced ref callback with cleanup
const scrollButtonRef = (element: HTMLButtonElement | null) => {
if (element) {
// Setup element-specific logic
element.setAttribute('data-component', 'back-to-top');
console.log('Back to top button mounted');
// Add focus management for accessibility
element.addEventListener('focus', () => {
element.style.outline = '2px solid #3b82f6';
});
element.addEventListener('blur', () => {
element.style.outline = 'none';
});
}
// Return cleanup function (React 19 feature)
return () => {
if (element) {
console.log('Back to top button cleanup');
element.removeAttribute('data-component');
// Event listeners on the element are automatically cleaned up
}
};
};
return <button ref={scrollButtonRef}>Back to Top</button>;
};
Key Benefits
- Automatic Cleanup: Cleanup functions are called when the component unmounts
- Better Memory Management: No risk of memory leaks from forgotten cleanup
- Cleaner Code: No need for separate useEffect for ref-related setup and cleanup
Feature 2: The use() Hook for Async Data Loading
React 19's use() hook can simplify Suspense-compatible async data loading by allowing components to read a promise and suspend until it is ready. It is a tool to apply where the surrounding data source and cache are designed for Suspense, not a requirement to replace every effect.
Before: Traditional useEffect Pattern
// Legacy async data loading with useEffect
const BlogPostPage = () => {
const [post, setPost] = useState<BlogPost | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const { slug } = useParams<{ slug: string }>();
useEffect(() => {
const loadPost = async () => {
try {
setLoading(true);
const blogPost = await getBlogPost(slug!);
setPost(blogPost);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load post');
} finally {
setLoading(false);
}
};
if (slug) {
loadPost();
}
}, [slug]);
if (loading) return <div>Loading...</div>;
if (error) return <div>Error: {error}</div>;
if (!post) return <div>Post not found</div>;
return <BlogPostContent post={post} />;
};
After: React 19 use() Hook
// React 19 use() hook for clean async data loading
const BlogPostContent = ({ slug }: { slug: string }) => {
// use() hook handles the async operation and suspends the component
const post = use(getBlogPost(slug));
if (!post) {
return <div>Post not found</div>;
}
return (
<article className="max-w-4xl mx-auto px-4 py-8">
{/* React 19 native metadata support */}
<title>{post.title} | Brett Sanders</title>
<meta name="description" content={post.excerpt} />
<meta name="keywords" content={post.keywords} />
<header className="mb-8">
<h1 className="text-4xl font-bold mb-4">{post.title}</h1>
<time className="text-gray-600 dark:text-gray-400">
{new Date(post.date).toLocaleDateString()}
</time>
</header>
<div
className="prose dark:prose-invert max-w-none"
dangerouslySetInnerHTML={{ __html: post.content }}
/>
</article>
);
};
const BlogPostPage = () => {
const { slug } = useParams<{ slug: string }>();
if (!slug) {
return <div>Invalid post URL</div>;
}
return (
<Suspense fallback={<div>Loading post...</div>}>
<BlogPostContent slug={slug} />
</Suspense>
);
};
Key Benefits
- Cleaner Code: No more complex loading states in components
- Better Error Handling: Errors are handled by Error Boundaries
- Automatic Suspense: Components automatically suspend until data is ready
- Improved Performance: React can optimize rendering with better scheduling
Feature 3: Simplified Context Providers
React 19 simplifies Context provider syntax by allowing direct usage of Context components without the .Provider suffix.
Before: Traditional Context Provider
// Legacy Context provider pattern
const ThemeContext = createContext<ThemeContextType | undefined>(undefined);
export const ThemeProvider: React.FC<ThemeProviderProps> = ({ children }) => {
const [theme, setTheme] = useState<Theme>('light');
const toggleTheme = () => {
setTheme(prev => prev === 'light' ? 'dark' : 'light');
};
return (
<ThemeContext.Provider value={{ theme, toggleTheme }}>
{children}
</ThemeContext.Provider>
);
};
After: React 19 Simplified Provider
// React 19 simplified Context provider
const ThemeContext = createContext<ThemeContextType | undefined>(undefined);
export const ThemeProvider: React.FC<ThemeProviderProps> = ({ children }) => {
const [theme, setTheme] = useState<Theme>('light');
const toggleTheme = () => {
setTheme(prev => prev === 'light' ? 'dark' : 'light');
};
// React 19: No need for .Provider suffix
return (
<ThemeContext value={{ theme, toggleTheme }}>
{children}
</ThemeContext>
);
};
Key Benefits
- Cleaner Syntax: Less verbose provider declarations
- Better Developer Experience: More intuitive API
- Future-Proof: Aligns with React's direction toward simpler APIs
Feature 4: Native Metadata Support
React 19 introduces native support for document metadata, allowing components to declaratively set <title>, <meta>, and other head elements without third-party libraries.
The current portfolio uses native metadata elements and keeps Helmet as a compatibility fallback while the site remains a client-rendered Vite application.
Before: Third-Party Library Dependency
// Legacy approach with react-helmet-async
import { Helmet } from 'react-helmet-async';
const BlogPost = ({ post }: { post: BlogPost }) => {
return (
<>
<Helmet>
<title>{post.title} | Brett Sanders</title>
<meta name="description" content={post.excerpt} />
<meta name="keywords" content={post.keywords} />
</Helmet>
<article>{/* content */}</article>
</>
);
};
After: React 19 Native Metadata
// React 19 native metadata support
const BlogPost = ({ post }: { post: BlogPost }) => {
return (
<article>
{/* React 19 native metadata - automatically hoisted to <head> */}
<title>{post.title} | Brett Sanders</title>
<meta name="description" content={post.excerpt} />
<meta name="keywords" content={post.keywords} />
{/* Component content */}
<header>
<h1>{post.title}</h1>
</header>
<div dangerouslySetInnerHTML={{ __html: post.content }} />
</article>
);
};
Key Benefits
- Platform Capability: Metadata hoisting is built into React 19, even though this site currently retains a compatibility fallback
- Automatic Hoisting: Metadata elements are automatically moved to
<head> - Better Performance: No JavaScript overhead for metadata management
- SEO Friendly: Metadata is available immediately when the page loads
Feature 5: Intentional Performance Optimizations
React 19 does not automatically replace useCallback, useMemo, or React.memo. The separate React Compiler can add automatic memoization when it is installed and configured. Without the compiler, the right approach is still to profile, keep memoization where it has a clear purpose, and simplify it where it does not.
Before: Manual Memoization
// Legacy approach with manual memoization
const BackToTop = () => {
const [isVisible, setIsVisible] = useState(false);
const handleScroll = useCallback(() => {
const shouldShow = window.scrollY > 300;
setIsVisible(prev => prev !== shouldShow ? shouldShow : prev);
}, []);
const scrollToTop = useCallback(() => {
window.scrollTo({ top: 0, behavior: 'smooth' });
}, []);
useEffect(() => {
let ticking = false;
const throttledScroll = () => {
if (!ticking) {
requestAnimationFrame(() => {
handleScroll();
ticking = false;
});
ticking = true;
}
};
window.addEventListener('scroll', throttledScroll, { passive: true });
return () => window.removeEventListener('scroll', throttledScroll);
}, [handleScroll]);
return (
<button onClick={scrollToTop}>
Back to Top
</button>
);
};
After: Simpler Component-Local Functions
const BackToTop = () => {
const [isVisible, setIsVisible] = useState(false);
useEffect(() => {
// Local to the effect, so it is not an external dependency.
const handleScroll = () => {
const shouldShow = window.scrollY > 300;
setIsVisible(prev => prev !== shouldShow ? shouldShow : prev);
};
// Throttle scroll events for better performance
let ticking = false;
const throttledScroll = () => {
if (!ticking) {
requestAnimationFrame(() => {
handleScroll();
ticking = false;
});
ticking = true;
}
};
window.addEventListener('scroll', throttledScroll, { passive: true });
return () => window.removeEventListener('scroll', throttledScroll);
}, []); // Empty dependency array since handleScroll is defined inside
const scrollToTop = () => {
window.scrollTo({ top: 0, behavior: 'smooth' });
};
return (
<button onClick={scrollToTop}>
Back to Top
</button>
);
};
Key Benefits
- Cleaner Code: Less boilerplate for performance optimization
- Correct Dependencies: Functions used only by an effect can live inside that effect
- Measured Performance: Memoization stays reserved for demonstrated render or identity needs
- Reduced Bundle Size: Less code needed for manual optimizations
Modern TypeScript Configuration
To fully leverage React 19's capabilities, I updated my TypeScript configuration to use modern standards:
{
"compilerOptions": {
"target": "ES2022",
"lib": ["ES2022", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"isolatedModules": true,
"moduleDetection": "force",
"noEmit": true,
"jsx": "react-jsx",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"exactOptionalPropertyTypes": true,
"noUncheckedIndexedAccess": true
}
}
Key Configuration Changes
- ES2022 Target: Modern JavaScript features and optimizations
- Strict Type Checking: Better error detection and type safety
- Exact Optional Properties: Prevents common TypeScript gotchas
- Unchecked Index Access: Enabling this option adds stricter checks for array and object access, making them safer by preventing unchecked access to potentially undefined elements
Measuring the Impact
After implementing these React 19 best practices, I observed several improvements:
Performance Gains
- Reduced Bundle Size: Route-level code splitting and dependency cleanup reduced overall JavaScript size
- Faster Rendering: Enhanced scheduling and batching improved page responsiveness
- Better Memory Management: Enhanced ref callbacks eliminated potential memory leaks
Developer Experience Improvements
- Cleaner Codebase: Simplified patterns reduced cognitive load
- Better Error Messages: React 19's improved error handling made debugging easier
- Enhanced TypeScript Support: Better type inference and error detection
SEO and Accessibility Benefits
- Improved SEO: Native metadata support enhanced search engine optimization
- Better Accessibility: Enhanced focus management and cleanup functions
- Faster Initial Load: Optimized rendering improved Core Web Vitals
Challenges and Solutions
Challenge 1: TypeScript Compatibility
Some React 19 features required TypeScript configuration updates to work properly.
Solution: Updated tsconfig.json with modern settings and added strict type checking options.
Challenge 2: Suspense Integration
The use() hook requires proper Suspense boundary setup, which needed careful planning.
Solution: Added strategic Suspense boundaries and proper error boundaries for robust async data loading.
Challenge 3: Legacy Code Coexistence
Maintaining compatibility with existing patterns while introducing new ones.
Solution: Gradual migration approach, updating components one by one while maintaining backward compatibility.
Best Practices for React 19 Adoption
Based on my experience, here are key recommendations for adopting React 19 best practices:
1. Start with TypeScript Configuration
Update your TypeScript configuration first to get better type checking and error detection.
2. Implement Enhanced Ref Callbacks Gradually
Start with components that have cleanup requirements, then expand to others.
3. Use the use() Hook for New Async Operations
Apply the use() hook to new components first, then gradually refactor existing ones.
4. Remove Manual Memoization Strategically
Test performance before and after removing useCallback and useMemo. If you enable React Compiler, verify its output separately; do not assume the React 19 runtime memoizes components on its own.
5. Plan Suspense Boundaries Carefully
Design your component hierarchy with proper Suspense boundaries for optimal user experience.
Future-Proofing Your React Applications
React 19 represents a significant step forward in React's evolution. By adopting these patterns now, you're preparing your applications for:
- Better Performance Tooling: React 19.2 performance tracks and an optional stable React Compiler for measured optimization work
- Enhanced Developer Experience: Cleaner, more intuitive APIs
- Improved SEO: Native metadata support for better search engine optimization
- Better Accessibility: Enhanced focus management and cleanup patterns
Conclusion
Implementing React 19 best practices in my portfolio application was both challenging and rewarding. The new features provide significant improvements in code quality, performance, and developer experience while maintaining backward compatibility.
The key takeaway is that React 19 isn't just about new features—it's about enabling better development patterns that make our applications more maintainable, performant, and user-friendly. By adopting these practices now, we're building applications that are ready for the future of React development.
Whether you're starting a new project or upgrading an existing application, React 19's features provide compelling reasons to modernize your development approach. The investment in learning these patterns pays dividends in code quality, performance, and long-term maintainability.