React Teleporter is a library designed for seamlessly moving or "teleporting" React components within the same React component tree. Unlike standard React Portals, which can render children into a different DOM node outside the current DOM hierarchy, `react-teleporter` maintains the logical connection within the same React tree. This distinction simplifies state and context management, as components remain within their original React context, making it ideal for managing complex layouts where content needs to appear in a different visual location than where it's defined. Inspired by the configuration philosophy of `react-helmet`, it allows for configuring parts of an application from a separate, perhaps deeply nested, location. The current stable version is 3.2.0, with regular minor and patch releases to support new React versions (currently up to React 19) and add features like `function as children` for `Source` components. Major versions, such as v3.0.0, introduce significant breaking changes, notably the transition to an ESM-only distribution. The library exports `createTeleporter`, which generates a `Source` and `Target` pair, enabling content defined within a `Source` component to be rendered at the `Target`'s designated position.
npm install react-teleporterVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `createTeleporter` to define a `StatusBar` teleporter, allowing content from a `Source` component within `Page` to be rendered at a `Target` location inside `Header`, effectively moving UI elements across the React tree without prop drilling.
Update your import statements from `const { createTeleporter } = require('react-teleporter');` to `import { createTeleporter } from 'react-teleporter';` and ensure your build setup supports ESM.Review your TypeScript code and update type annotations and usage of `createTeleporter` and its returned components according to the new type definitions. Refer to the official documentation or example usages for v3.x.
Ensure that if you specify a custom DOM element with `as`, it correctly handles refs as expected by React Portals, or use `useTargetRef` for custom target elements where direct DOM manipulation is required.
To enable multiple `Source` components to render content simultaneously at a `Target`, initialize your teleporter with the `multiSources` option: `const Teleporter = createTeleporter({ multiSources: true });`.If using a functional child for `Source`, ensure your `react-teleporter` version is 3.1.0 or newer. Example: `<Teleporter.Source>{(element) => <div onClick={forwardEvent(element)}></div>}</Teleporter.Source>`.Change your import statement from `const { createTeleporter } = require('react-teleporter');` to `import { createTeleporter } from 'react-teleporter';`.Ensure you are using `react-teleporter@^3.0.0` or later and update your TypeScript configuration and code to reflect the new type definitions. Run `npm install react-teleporter@latest` or `yarn add react-teleporter@latest`.
Initialize your teleporter with `multiSources: true`: `const MyTeleporter = createTeleporter({ multiSources: true });`.