DEV Community

Cover image for Optimizing Expensive React Components with React.memo and useMemo
HexShift
HexShift

Posted on

Optimizing Expensive React Components with React.memo and useMemo

Sometimes components need to talk to each other without being parent-child related. Instead of prop-drilling or overusing global state, you can build a lightweight event bus pattern in React — perfect for modals, toasts, and real-time collaboration features.

Step 1: Create a Simple Event Emitter


We'll make a minimal pub-sub (publish/subscribe) system.

class EventBus {
constructor() {
this.listeners = {};
}

on(event, callback) {
(this.listeners[event] ||= []).push(callback);
}

off(event, callback) {
if (!this.listeners[event]) return;
this.listeners[event] = this.listeners[event].filter(cb => cb !== callback);
}

emit(event, payload) {
(this.listeners[event] || []).forEach(cb => cb(payload));
}
}

export const eventBus = new EventBus();

Step 2: Fire Events from Anywhere


Any component can now broadcast events across the app.

import { eventBus } from "./eventBus";

function Publisher() {
return (
<button onClick={() => eventBus.emit("greet", "Hello World!")}>
Send Greeting
</button>
);
}

Step 3: Listen for Events in Any Component


Use useEffect to subscribe and clean up properly.

import { useEffect, useState } from "react";
import { eventBus } from "./eventBus";

function Subscriber() {
const [message, setMessage] = useState("");

useEffect(() => {
const handler = (payload) => setMessage(payload);
eventBus.on("greet", handler);
return () => eventBus.off("greet", handler);
}, []);

return <div>Received: {message}</div>;
}

✅ Pros


  • Instant, decoupled communication across the app.
  • No context providers, Redux, or complex wiring needed.
  • Extremely lightweight (a few dozen lines).

⚠️ Cons


  • No built-in type safety (consider extending for TS users).
  • Harder to trace event flows in large apps without discipline.
  • Not ideal for critical app-wide state (use Context or Redux there).

Wrap-Up


The event bus pattern is perfect for lightweight, decoupled messaging between parts of a React app. It's especially useful for things like modals, notifications, game state, or real-time features where clean hierarchy isn't possible or desirable.

Enjoyed this technique? Feel free to support me here: Buy Me a Coffee

Image of PulumiUP 2025

From Cloud to Platforms: What Top Engineers Are Doing Differently

Hear insights from industry leaders about the current state and future of cloud and IaC, platform engineering, and security.

Save Your Spot

Top comments (0)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay