@propero/react-replace-component
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

react-replace-component

Replace react components dynamically at a later time

npm i @propero/react-replace-component

Getting started

import { createComponentStore } from "@propero/react-replace-component";

const { useComponent, useComponents, registerComponent, setNamespace } = createComponentStore();

export function Target() {
  return <div className="a" />;
}


export function AnotherComponent() {
  return <div className="aa" />;
}

export function Replacement() {
  return <div className="b" />;
}

export function MyPage() {
  // This allows the component to be replaced at a later time
  const TargetEx = useComponent(Target);
  return <TargetEx />;
}

export function MultipleComponents() {
  // Allows for replacing multiple components, each one can be replaced individually still.
  // Components are suffixed with Ex to avoid shadowing or having to rename properties in destructuring.
  const { TargetEx, AnotherComponentEx } = useComponents({ Target, AnotherComponent });
  return <>
    <TargetEx />
    <AnotherComponentEx />
  </>;
}

// This replaces the Target components with Replacement components.
// Only works if current namespace is default
registerComponent(Target, Replacement);

// Switch themes/extensions/customer specific replacements quickly with namespaces
setNamespace("foo")

Package Sidebar

Install

npm i @propero/react-replace-component

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

23.4 kB

Total Files

11

Last publish

Collaborators

  • eyyy-nora
  • merlin_r
  • nschad