@memberjunction/ng-react
An Angular integration library for rendering React components inside Angular applications. Provides a bridge component, script loader, and debugging tools for embedding React-based UI within the MemberJunction Angular ecosystem.
Installation
Section titled “Installation”npm install @memberjunction/ng-reactOverview
Section titled “Overview”This package provides a seamless bridge between Angular and React, allowing React components to be loaded and rendered within Angular templates. It handles React and ReactDOM script loading, Babel transpilation of JSX, component lifecycle management, and two-way communication between the Angular host and React guest.
flowchart TD
subgraph Angular["Angular Host"]
A["MJReactComponent"] -->|Props| B["ReactBridgeService"]
end
subgraph Bridge["Bridge Layer"]
B --> C["ScriptLoaderService"]
C --> D["Load React + ReactDOM + Babel"]
B --> E["AngularAdapterService"]
end
subgraph React["React Guest"]
D --> F["React Component"]
F -->|Events| E
E -->|Callbacks| A
end
style Angular fill:#2d6a9f,stroke:#1a4971,color:#fff
style Bridge fill:#7c5295,stroke:#563a6b,color:#fff
style React fill:#2d8659,stroke:#1a5c3a,color:#fff
Module Import
Section titled “Module Import”import { ReactModule } from '@memberjunction/ng-react';
@NgModule({ imports: [ReactModule]})export class YourModule {}Basic React Component Rendering
Section titled “Basic React Component Rendering”<mj-react-component [ComponentCode]="reactComponentCode" [Props]="componentProps" (ComponentEvent)="handleReactEvent($event)" (StateChange)="handleStateChange($event)" (ComponentError)="handleError($event)"></mj-react-component>Providing React Code
Section titled “Providing React Code”reactComponentCode = ` function MyComponent({ title, onAction }) { const [count, setCount] = React.useState(0); return ( <div> <h2>{title}</h2> <p>Count: {count}</p> <button onClick={() => setCount(c => c + 1)}>Increment</button> <button onClick={() => onAction('submit')}>Submit</button> </div> ); }`;
componentProps = { title: 'My React Widget', onAction: (action: string) => console.log('React action:', action)};Components
Section titled “Components”| Component | Selector | Purpose |
|---|---|---|
MJReactComponent | mj-react-component | Renders a React component inside Angular |
MJReactComponent Inputs
Section titled “MJReactComponent Inputs”| Property | Type | Default | Description |
|---|---|---|---|
ComponentCode | string | '' | JSX/React code to transpile and render |
Props | Record<string, unknown> | {} | Props passed to the React component |
EnableDebug | boolean | false | Enable debug logging |
MJReactComponent Outputs
Section titled “MJReactComponent Outputs”| Event | Type | Description |
|---|---|---|
ComponentEvent | EventEmitter<ReactComponentEvent> | Custom events from the React component |
StateChange | EventEmitter<StateChangeEvent> | React component state changes |
ComponentError | EventEmitter<Error> | Errors during rendering or transpilation |
ComponentReady | EventEmitter<void> | React component has mounted |
MJReactComponent Methods
Section titled “MJReactComponent Methods”Hosts that drive a component from outside (an agent operating it through a realtime channel, a toolbar) call the standard component methods on the MJReactComponent instance: InvokeMethod(name, ...args), HasMethod(name), GetCurrentDataState(), Refresh() (the component’s own refresh() when it registered one, otherwise a re-render), Print(), validate(), IsDirty(), Reset(), ScrollTo(target) and Focus(target?). Each is a no-op (or undefined/false) until the component has initialized (Initialized fires). A new Component input is a new version: the component reinitializes in place and Initialized fires again.
PDF Export
Section titled “PDF Export”CapturePDF(orientation = 'portrait', title?) downloads the rendered component as an A4 PDF named <title>_<date>.pdf (the title defaults to the spec’s title or name). The artifact viewer offers it from its header as PDF – Portrait / PDF – Landscape.
- The component is laid out at the page’s printable width (718 CSS px portrait, 1047 landscape) so it prints the same whatever the viewer’s size; content wider than that (a wide table) is laid out wider and scaled down to fit. The host page around the component is not changed.
- Content behind inner scrollbars is included, Chart.js charts are re-rendered at 3x, and
data-export-control="true"elements (export buttons) are left out. - Page breaks avoid splitting charts, rows, form fields and text blocks, keep a table’s lead-in (title, search box, header row) with its first row and headings with what follows, and shrink a page by up to 20% when that keeps it from being left mostly empty.
- Content rendered in portals (antd modals, drawers) is outside the component and not included. Each page is one image, so text in the PDF is not selectable.
The pieces are exported for reuse: ExportComponentToPDF(element, title, orientation), PlanPdfPages, EnterPrintMode, and BuildImagePdf (a minimal JPEG-per-page PDF writer).
Services
Section titled “Services”| Service | Purpose |
|---|---|
ScriptLoaderService | Loads React, ReactDOM, and Babel from CDN with caching |
ReactBridgeService | Manages React component lifecycle and prop synchronization |
AngularAdapterService | Provides Angular context (router, DI) to React components |
Configuration
Section titled “Configuration”Debug Configuration
Section titled “Debug Configuration”import { ReactDebugConfig } from '@memberjunction/ng-react';
// Enable verbose logging for developmentReactDebugConfig.enabled = true;ReactDebugConfig.logLevel = 'verbose';How It Works
Section titled “How It Works”ScriptLoaderServiceloads React, ReactDOM, and Babel scripts if not already present- The React component code (JSX string) is transpiled using Babel in the browser
ReactBridgeServicecreates a React root and renders the component into a DOM container- Props from Angular are passed to React; events from React are forwarded to Angular
- On Angular component destruction, the React tree is properly unmounted
Dependencies
Section titled “Dependencies”- @memberjunction/global — Global utilities
- React and ReactDOM (loaded at runtime from CDN)
- Babel standalone (loaded at runtime for JSX transpilation)
- html-to-image — DOM capture for PDF export