React Kapsule Wrapper
react-kapsule is a utility library designed to integrate Kapsule-style web components into React applications. It provides a higher-order component, `fromKapsule`, which acts as a bridge, transforming a Kapsule component definition (often a closure-based functional component following Mike Bostock's reusable charts pattern) into a standard React component. The current stable version is 2.5.7, with the last publish being approximately a year ago, indicating a mature and stable library rather than one with frequent, breaking updates. It ships with TypeScript types, ensuring robust type checking for consumers. Its core utility lies in abstracting the imperative nature of Kapsule components, allowing them to be declaratively used within React's component tree, handling prop updates and method invocations transparently. This enables developers to leverage existing Kapsule components or build new ones with Kapsule, while still utilizing React for their main application UI.
Common errors
-
TypeError: (0, react_kapsule__WEBPACK_IMPORTED_MODULE_0__.fromKapsule) is not a function
cause Attempting to import `fromKapsule` as a named export instead of a default export in an ES module environment.fixChange the import statement to `import fromKapsule from 'react-kapsule';` (removing the curly braces). -
Module not found: Can't resolve 'react-kapsule'
cause The 'react-kapsule' package is not installed or incorrectly referenced in your project's dependencies.fixInstall the package using your package manager: `npm install react-kapsule` or `yarn add react-kapsule`. Ensure it's listed in `package.json`. -
Cannot read properties of undefined (reading 'increment')
cause Attempting to call a Kapsule method (e.g., `increment`) on the wrapped React component without a valid ref, or before the ref has been assigned (e.g., during initial render before mount).fixEnsure the ref is correctly attached to the `MyReactKapsuleComponent` and checked for `null` or `undefined` before attempting to call methods. Methods are only available after the component has mounted.
Warnings
- gotcha Props specified in `options.initPropNames` are only used during the initial instantiation of the Kapsule component. Modifying these props after the component has mounted will have no effect on the underlying Kapsule component's configuration.
- gotcha Methods of the underlying Kapsule component, when exposed via `options.methodNames`, are accessible only through a React ref to the wrapped component instance. They are not directly available as props.
- breaking This package lists 'react' as a peer dependency, requiring `react@>=16.13.1`. Using an incompatible version of React (e.g., older than 16.13.1) can lead to unexpected behavior or runtime errors.
Install
-
npm install react-kapsule -
yarn add react-kapsule -
pnpm add react-kapsule
Imports
- fromKapsule
import { fromKapsule } from 'react-kapsule';import fromKapsule from 'react-kapsule';
- fromKapsule (CJS)
const { fromKapsule } = require('react-kapsule');const fromKapsule = require('react-kapsule'); - Kapsule (from parent library)
import { Kapsule } from 'kapsule';import Kapsule from 'kapsule';
Quickstart
import React, { useRef, useEffect } from 'react';
import ReactDOM from 'react-dom/client';
import Kapsule from 'kapsule';
import fromKapsule from 'react-kapsule';
// Define a simple Kapsule component
const myKapsuleComponent = Kapsule({
props: {
message: { default: 'Hello' },
count: { default: 0 }
},
methods: {
increment: function(state) { state.count++; },
setMessage: function(state, msg) { state.message = msg; }
},
init(domNode, state) {
state.container = document.createElement('div');
domNode.appendChild(state.container);
},
update(state) {
state.container.innerHTML = `<span>${state.message} from Kapsule! Count: ${state.count}</span>`;
}
});
// Wrap the Kapsule component for React
const MyReactKapsuleComponent = fromKapsule(myKapsuleComponent, {
methodNames: ['increment', 'setMessage'] // Expose Kapsule methods as React component methods
});
function App() {
const kapsuleRef = useRef(null);
useEffect(() => {
const interval = setInterval(() => {
if (kapsuleRef.current) {
(kapsuleRef.current as any).increment(); // Call exposed Kapsule method
}
}, 1000);
return () => clearInterval(interval);
}, []);
return (
<div>
<h1>React Kapsule Example</h1>
<MyReactKapsuleComponent
ref={kapsuleRef}
message="Greetings"
count={10}
/>
<button onClick={() => (kapsuleRef.current as any)?.setMessage('New message!')}>
Change Message
</button>
</div>
);
}
const rootElement = document.getElementById('root');
if (rootElement) {
const root = ReactDOM.createRoot(rootElement);
root.render(<App />);
} else {
console.error("Root element not found");
}