single-spa-react
repository·main·Indexed 42 days ago
https://github.com/single-spa/single-spa-reactA wrapper providing single-spa lifecycle hooks for React applications, allowing them to be used as applications or parcels within a microfrontend architecture. Version 7.0.0-beta.0 includes the singleSpaReact function for managing bootstrap, mount, unmount, and update lifecycles, and the SingleSpaReactParcel component for rendering single-spa parcels within React apps.
What's inside single-spa-react
- single-spa-react provides generic lifecycle hooks for React applications. It allows you to register React components as either single-spa applications or single-spa parcels. This enables React code to participate in a single-spa microfrontend architecture by managing the mounting, unmounting, and updating of React components within the single-spa lifecycle.
Use singleSpaReact to create single-spa lifecycles
mainThe
singleSpaReactfunction is the primary entrypoint for creatingsingle-spalifecycle functions (bootstrap,mount,unmount,update) for a React application. It abstracts the complexity of managing React roots (usingcreateRootorhydrateRoot) within the single-spa ecosystem.To use it, you must provide a configuration object of type
SingleSpaReactOptsthat includes React's core functions and a rendering function.import singleSpaReact from 'single-spa-react'; import React from 'react'; import { createRoot } from 'react-dom/client'; const lifecycles = singleSpaReact({ createElement: React.createElement, useEffect: React.useEffect, renderReactNode: (props) => <MyAppComponent {...props} />, createRoot: createRoot, // Optional: rootOptions, domElementGetter, etc. }); export const { bootstrap, mount, unmount } = lifecycles;Use the SingleSpaReactParcel component
mainThe
SingleSpaReactParcelcomponent is used to render asingle-spaparcel within a React application. It manages the lifecycle of the parcel, including loading its configuration, mounting it into a DOM element, updating it when props change, and unmounting it when the component is destroyed.To use it, you must provide a
configprop (either aParcelConfigobject or a function that returns aPromise<ParcelConfig>) and a way to mount the parcel, either via themountParcelprop or by providing asingleSpaContext.import SingleSpaReactParcel from 'single-spa-react/Parcel'; // Example using a config object and mountParcel function <SingleSpaReactParcel config={{ selector: '#my-parcel', module: () => import('my-parcel-module'), }} mountParcel={mountRootParcel} wrapWith="div" />Configure SingleSpaReactParcelProps
mainThe
SingleSpaReactParcelPropsinterface defines the configuration options for the<SingleSpaReactParcel />component:Prop Type Description configParcelConfig | (() => Promise<ParcelConfig>)Required. The configuration for the parcel. Can be a direct object or an async loading function. mountParceltypeof mountRootParcelThe function used to mount the parcel. If not provided, it attempts to use mountParcelfrom thesingleSpaContext.singleSpaContextContext<AppProps>A React context containing single-spaapplication properties, includingmountParcel.parcelDidMount() => voidCallback function executed when the parcel's mountPromiseresolves.parcelDidUpdate() => voidCallback function executed after the parcel's updatemethod resolves.handleError(err: Error) => voidCallback function to handle errors during config loading, mounting, or updating. wrapWithstringThe HTML element tag used to wrap the parcel container. Defaults to "div".wrapWithPropsDetailedHTMLProps<any, any>Additional props to apply to the wrapWithelement....parcelPropsanyAny additional props passed directly to the parcel's mountandupdatemethods.Configure SingleSpaReactOpts
mainThe
SingleSpaReactOptsobject defines how the React application is rendered and managed.Required Properties
createElement: The ReactcreateElementfunction.useEffect: The ReactuseEffecthook.renderReactNode: A function that returns aReactNodeor aPromise<ReactNode>. This function is responsible for generating the element tree to be rendered.createRootORhydrateRoot: You must provide eithercreateRoot(for client-side rendering) orhydrateRoot(for server-side rendering/hydration) fromreact-dom/client.
Optional Properties
rootOptions: Configuration passed to React'screateRootorhydrateRoot(e.g.,onUncaughtError).domElementGetter: Options for determining which DOM element the React application should attach to (inherited fromDomElementGetterOpts).