React › Render Props Pattern
What is a simple real world example of the render props pattern being useful?
BeginnerAnswer
A component that tracks whether the browser window is online or offline could use render props, letting different parts of an app show that information in completely different ways, such as a small icon in one place and a full banner message in another, all powered by the exact same shared logic.
function OnlineStatus({ render }) {
const [isOnline, setIsOnline] = useState(navigator.onLine);
useEffect(() => {
const goOnline = () => setIsOnline(true);
const goOffline = () => setIsOnline(false);
window.addEventListener('online', goOnline);
window.addEventListener('offline', goOffline);
return () => {
window.removeEventListener('online', goOnline);
window.removeEventListener('offline', goOffline);
};
}, []);
return render(isOnline);
}Real-world example A messaging app uses a shared OnlineStatus component to show a small green dot in the header and a full warning banner at the top of the chat screen, both powered by the exact same online tracking logic through render props.