Version: 5.x

single-spa-react

Build Status

single-spa-react is a helper library that helps implement single-spa registered application lifecycle functions (bootstrap, mount and unmount) for use with React. Check out the single-spa-react github.

  1. npm install --save single-spa-react
  2. # or
  3. yarn add single-spa-react

Alternatively, you can use single-spa-react by adding <script src="https://unpkg.com/single-spa-react"></script> and accessing the singleSpaReact global variable.

Use single-spa-react to create and export single-spa lifecycle functions from your application’s entry file.

  1. import React from 'react';
  2. import ReactDOMClient from 'react-dom/client';
  3. import rootComponent from './path-to-root-component.js';
  4. // SingleSpaContext is a react@16.3 (if available) context that provides singleSpa props
  5. import singleSpaReact, { SingleSpaContext } from 'single-spa-react';
  6. export const { bootstrap, mount, unmount } = singleSpaReact({
  7. React,
  8. ReactDOMClient,
  9. rootComponent,
  10. errorBoundary(err, info, props) {
  11. // https://reactjs.org/docs/error-boundaries.html
  12. return <div>This renders when a catastrophic error occurs</div>;
  13. },
  14. });

Quickstart for React <=17

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import rootComponent from './path-to-root-component.js';
  4. import singleSpaReact, { SingleSpaContext } from 'single-spa-react';
  5. export const { bootstrap, mount, unmount } = singleSpaReact({
  6. React,
  7. ReactDOM,
  8. rootComponent,
  9. errorBoundary(err, info, props) {
  10. // https://reactjs.org/docs/error-boundaries.html
  11. return <div>This renders when a catastrophic error occurs</div>;
  12. },
  13. });

All options are passed to single-spa-react via the opts parameter when calling singleSpaReact(opts). The following options are available:

  • React: (required) The main React object, which is generally either exposed onto the window or is available via require('react') import React from 'react'.
  • ReactDOM: (required) The main ReactDOMbject, which is available via require('react-dom') import ReactDOM from 'react-dom'.
  • rootComponent: (required) The top level React component which will be rendered. Can be omitted only if loadRootComponent is provided.
  • loadRootComponent: (optional) A loading function that takes custom single-spa props and returns a promise that resolves with the parcel. This takes the place of the rootComponent opt, when provided. It is intended to help people who want to lazy load the source code for their root component. The source code will be lazy loaded during the bootstrap lifecycle.
  • errorBoundary: (optional) A function that accepts err, info, and props and must return the UI for a React Error Boundary. This is provided as a convenient way of implementing an Error boundary without having to write your own class component for it. The errorBoundary function may be provided in the options to singleSpaReact(), or as a custom prop provided by the root config.
  • errorBoundaryClass: (optional) A React Component class that will be used as the React error boundary. This is an alternative to providing the errorBoundary function. The errorBoundaryClass may be provided in the options to singleSpaReact(), or as a custom prop provided by the root config.
  • suppressComponentDidCatchWarning: (optional) A boolean that indicates if single-spa-react should warn when the rootComponent does not implement componentDidCatch. Defaults to false. It is preferred to implement errorBoundary instead of suppressing this warning.
  • domElementGetter: (optional) A function that is given the single-spa props and returns a DOMElement. This dom element is where the React application will be bootstrapped, mounted, and unmounted. Note that this opt can be omitted. When omitted, the domElementGetter or domElementcustom single-spa props are used. To use those, do singleSpa.registerApplication({ name, app, activeWhen, customProps: {domElementGetter: function() {...}} }) or singleSpa.registerApplication({ name, app, activeWhen, {domElement: document.getElementById(...)} }). If no dom element can be found through any of those methods, then a container div will be created and appended to document.body, by default.
  • parcelCanUpdate: (optional) A boolean that controls whether an update lifecycle will be created for the returned parcel. Note that option does not impact single-spa applications, but only parcels. It is true by default.
  • renderType: (optional) ENUM of one of the following: 'render', 'hydrate', 'createRoot', 'unstable_createRoot', 'createBlockingRoot', and 'unstable_createBlockingRoot'. Defaults to 'render'. Allows you to choose which ReactDOM render method you want to use for your application. As of single-spa-react@4.6.0, renderType can be a function that returns a string, for dynamically calculated renderType.

For react@>=16, it is best practice to have each single-spa application’s root application implement componentDidCatch in order to avoid the entire application unmounting unexpectedly when an error occurs. single-spa-react will warn to the console if componentDidCatch is not implemented. See https://reactjs.org/blog/2017/07/26/error-handling-in-react-16.html for more details.

single-spa-react can also be used to create a single-spa parcel (instead of a single-spa application). To do so, simply call singleSpaReact() the same as for an application, except without a domElementGetter (since those are provided by the code that will mount the parcel).

Additionally, single-spa-react provides a <Parcel> component to make using framework agnostic single-spa parcels easier. This allows you to put the parcel into your render method’s jsx, instead of having to implement componentDidMount and componentWillUnmount. You can use the Parcel component either by npm installing the library and importing single-spa-react/parcel or by adding <script src="https://unpkg.com/single-spa-react/parcel"></script> and then accessing the Parcel component with window.Parcel.default.

  • config (required): Either a single-spa parcel config object, or a “loading function” that returns a Promise that resolves with the parcel config.
  • wrapWith (optional): A string tagName.<Parcel> will create a dom node of that type for the parcel to be mounted into. Defaults to div
  • wrapStyle(optional): Styles that will apply to wrapWith.
  • wrapClassName (optional): classNames that will apply to wrapWith.
  • appendTo (optional): A dom element to append the parcel to. By default, this is not needed because the parcel will be mounted in the DOM that the <Parcel> component was rendered into. Useful for appending parcels to document.body or other separate parts of the dom.
  • mountParcel (sometimes required, sometimes not): The mountParcel function provided by single-spa. In general, it is preferred to use an application’s mountParcel function instead of the single-spa’s root mountParcel function, so that single-spa can keep track of the parent-child relationship and automatically unmount the application’s parcels when the application unmounts. Note that if the <Parcel> component is being rendered by a single-spa application that uses single-spa-react, it is unnecessary to pass in the prop, since <Parcel> can get the prop from SingleSpaContext
  • handleError (optional): A function that will be called with errors thrown by the parcel. If not provided, errors will be thrown on the window, by default.
  • parcelDidMount (optional): A function that will be called when the parcel finishes loading and mounting.
  1. // Use this import path in environments that support package.json exports
  2. // See https://nodejs.org/dist/latest-v14.x/docs/api/packages.html#packages_package_entry_points
  3. // and see https://github.com/single-spa/single-spa-react/releases/tag/v3.0.0
  4. // Use this in Webpack 5 and recent versions of Node
  5. import Parcel from 'single-spa-react/parcel'
  6. // Use this import path in environments that don't support package.json exports
  7. // See https://nodejs.org/dist/latest-v14.x/docs/api/packages.html#packages_package_entry_points
  8. // and see https://github.com/single-spa/single-spa-react/releases/tag/v3.0.0
  9. // Use this in Webpack 4 and older versions of Node
  10. import Parcel from 'single-spa-react/lib/esm/parcel'
  11. import * as parcelConfig from './my-parcel.js'
  12. // config is required. The parcel will be mounted inside of the
  13. // of a div inside of the react component tree
  14. <Parcel
  15. config={parcelConfig}
  16. wrapWith="div"
  17. handleError={err => console.error(err)}
  18. customProp1="customPropValue2"
  19. customProp2="customPropValue2"
  20. />
  21. // If you pass in an appendTo prop, the parcel will be mounted there instead of
  22. // to a dom node inside of the current react component tree
  23. <Parcel
  24. config={parcelConfig}
  25. wrapWith="div"
  26. appendTo={document.body}
  27. />
  28. // You can also pass in a "loading function" as the config.
  29. // The loading function must return a promise that resolves with the parcel config.
  30. // The parcel will be mounted once the promise resolves.
  31. <Parcel
  32. config={() => import('./my-parcel.js')}
  33. wrapWith="div"
  34. />
  35. // If you are rendering the Parcel component from a single-spa application, you do not need to pass a mountParcel prop.
  36. // But if you have a separate react component tree that is not rendered by single-spa-react, you **must** pass in a mountParcel prop
  37. // In general, it is preferred to use an application's mountParcel function instead of the single-spa's root mountParcel function,
  38. // so that single-spa can keep track of the parent-child relationship and automatically unmount the application's parcels when the application
  39. // unmounts
  40. <Parcel
  41. mountParcel={singleSpa.mountParcel}
  42. config={parcelConfig}
  43. wrapWith="div"
  44. />
  45. // Add styles to wrapWith element.
  46. <Parcel
  47. config={parcelConfig}
  48. wrapWith="div"
  49. wrapStyle={{ background: 'black' }}
  50. />
  51. // Add classNames to wrapWith element.
  52. <Parcel
  53. config={parcelConfig}
  54. wrapWith="div"
  55. wrapClassName="wrapper"
  56. />

See FAQ for CRA.

React - 图2Edit this page