DEV Community

OpenSource for Webcrumbs

Posted on • Edited on

Adding Components to React Without a Rebuild

Ah, the vast ocean of web development! It's where the waves of innovation never cease to roll. Among its many ships, React has carved a niche for being both sturdy and flexible. However, a common quill in the sails is the need to rebuild the entire ship whenever a new cannon (component) is added.

But what if there was a way to add firepower without returning to the dry dock?

This post researches a crafty method to add a component to a React application without invoking a rebuild or updating any library.

Imagine a bustling port representing your React application. Each dock is a component, contributing to the port's functionality. Now, you have a new vessel, an external component, ready to dock, but the idea of disturbing the existing setup for its accommodation seems tedious. The solution? A smooth integration without a rebuild!

The Independent Vessel: Server-Side Component

Start by hosting the new component on a separate server. Build and bundle it independently, ensuring it's ready for the high seas on its own. This independent vessel can then be called upon when needed.

// Hosted at https://external-component-server.com/component.js
const MyComponent = () => {
  return <div>Hello from the external component</div>;
};
window.MyExternalComponent = MyComponent;
Enter fullscreen mode Exit fullscreen mode

Navigating the Waters: Dynamic Script Loading

With the external component ready, it's time to navigate the waters. Utilize a dynamic script loading strategy to fetch and render the new component at runtime. This way, the new vessel sails and docks smoothly without disturbing the existing setup.

function loadComponent(src) {
  return new Promise((resolve, reject) => {
    const script = document.createElement('script');
    script.src = src;
    script.async = true;
    script.onload = () => { resolve(window.myExternalComponent); };
    script.onerror = () => { reject('Failed to load the component'); };
    document.body.appendChild(script);
  });
}

async function renderComponent() {
  try {
    const Component = await loadComponent('https://external-component-server.com/component.js');
    ReactDOM.render(<Component />, document.getElementById('component-container'));
  } catch (error) {
    console.error(error);
  }
}

renderComponent();
Enter fullscreen mode Exit fullscreen mode

The Grand Reception: Style Injection

A vessel arriving at a port is a spectacle. Ensure the new component matches the grandeur by dynamically injecting the necessary styles hosted on a CDN.

const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = 'https://cdn.example.com/component-styles.css';
document.head.appendChild(link);
Enter fullscreen mode Exit fullscreen mode

Engaging in Trade: Component Communication

Establishing a communication channel between your port and the new vessel is crucial for seamless interaction. Utilize Custom Events or postMessage for a dialogue between the existing application and the new component.

The journey of dynamically introducing a new component without a rebuild is like adding a fresh narrative to an already intriguing novel. It enriches the existing story while keeping the readers (users) engaged.

Explore WebCrumbs to dive deeper into the innovative realm of web development. Discover how modern developers are orchestrating seamless component integrations, and Join the Conversation to share your own tales of sailing the dynamic seas of React!

By embracing dynamic component loading, you're not just solving a technical challenge; you're weaving a tale of innovation, demonstrating the endless possibilities in the world of web development. Become a Maker with WebCrumbs, and let's continue crafting narratives that drive the web development saga forward!

Explore WebCrumbs

Top comments (0)