chayns API for React
The `chayns-api` package provides a React-centric interface for integrating applications with the chayns platform. It offers a comprehensive set of React hooks and utility functions to access chayns-specific information and functionalities, such as user data, location details, and various platform services. Currently stable at version 2.6.14, the library appears to be actively maintained, with regular updates indicated by its versioning and migration guides between major versions. Its primary differentiator is its deep integration with the chayns ecosystem, abstracting the complexities of the underlying platform API into idiomatic React patterns, making development for chayns applications more streamlined compared to direct API interactions. It requires `react` and `react-dom` as peer dependencies.
Common errors
-
Can't resolve 'react-dom/client'
cause Using `chayns-api` v2+ with React 17, which triggers a warning because `chayns-api` attempts to import `react-dom/client` (a React 18 feature).fixThis warning can be safely ignored as the library falls back to React 17's API. To remove the warning, upgrade your project to React 18 or higher. -
Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enabled for module scripts.
cause This error or similar module loading failures often occur after migrating to `chayns-api` v2 due to incorrect paths for the Module Federation entry file.fixEnsure all URLs referencing `remoteEntry.js` in your application configuration (e.g., webpack, ChaynsHost settings) have been updated to `v2.remoteEntry.js`.
Warnings
- breaking When migrating to `chayns-api` v2, you must also update `chayns-toolkit` to version `3.0.1` or higher. Additionally, URLs referencing `remoteEntry.js` must be updated to `v2.remoteEntry.js`.
- breaking Applications using `ChaynsHost` with a module-type in `chayns-api` v2+ might need to call `initModuleFederationSharing({ name: 'project_name' })` early in the application lifecycle (e.g., in `index`/`bootstrap` files) to ensure proper Module Federation.
- gotcha A warning "Can't resolve 'react-dom/client'" may appear when using `chayns-api` with React 17. This occurs because the library attempts to import React 18's client API.
Install
-
npm install chayns-api -
yarn add chayns-api -
pnpm add chayns-api
Imports
- ChaynsProvider
const ChaynsProvider = require('chayns-api').ChaynsProviderimport { ChaynsProvider } from 'chayns-api' - useUser
import useUser from 'chayns-api'
import { useUser } from 'chayns-api' - initModuleFederationSharing
const initModuleFederationSharing = require('chayns-api').initModuleFederationSharingimport { initModuleFederationSharing } from 'chayns-api'
Quickstart
import React from 'react';
import ReactDOM from 'react-dom/client';
import { ChaynsProvider, useUser, initModuleFederationSharing } from 'chayns-api';
// For applications utilizing ChaynsHost with module-type in v2+,
// it might be necessary to call initModuleFederationSharing early in your app.
// This is typically done in your entry point (e.g., index.ts/js or bootstrap.ts/js).
// Example: // initModuleFederationSharing({ name: 'your-project-name' });
const UserDisplay: React.FC = () => {
const user = useUser();
if (!user) {
return <p>Authenticating with chayns...</p>;
}
return (
<div style={{ padding: '20px', fontFamily: 'Arial, sans-serif' }}>
<h2>chayns User Profile</h2>
<p><strong>First Name:</strong> {user.firstName || 'N/A'}</p>
<p><strong>Last Name:</strong> {user.lastName || 'N/A'}</p>
<p><strong>Person ID:</strong> {user.personId}</p>
<p><strong>Location ID:</strong> {user.locationId}</p>
<p><strong>Admin Mode:</strong> {user.adminMode ? 'Yes' : 'No'}</p>
<p><strong>Is logged in:</strong> {user.loggedIn ? 'Yes' : 'No'}</p>
</div>
);
};
const App: React.FC = () => (
<ChaynsProvider>
<UserDisplay />
</ChaynsProvider>
);
const rootElement = document.getElementById('root');
if (rootElement) {
const root = ReactDOM.createRoot(rootElement);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
} else {
console.error("Root element with ID 'root' not found.");
}