chayns API for React

2.6.14 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

Demonstrates setting up the `ChaynsProvider` to enable chayns functionalities and using the `useUser` hook to display the current chayns user's profile information within a React component. It also hints at the `initModuleFederationSharing` function for v2+ migrations.

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.");
}

view raw JSON →