React context utilities for sharing data and state between framework and child applications.
React context for sharing data between framework and child applications.
/**
* React context for sharing data between framework and child applications
* Initialized with empty object by default
*/
export const FrameworkContext: React.Context<{}>;Hook for accessing framework context data with type safety.
/**
* Hook to access framework context with type safety
* @returns Typed context data from FrameworkContext
* @template T - Type of the context data object
*/
export function useFrameworkContext<T extends object>(): T;Usage Examples:
import { useFrameworkContext } from '@ice/plugin-icestark/Context';
// Define the expected context type
interface AppContextData {
theme: 'light' | 'dark';
user: { id: string; name: string };
permissions: string[];
}
// Use the context in a child application component
function ChildComponent() {
const contextData = useFrameworkContext<AppContextData>();
return (
<div className={`theme-${contextData.theme}`}>
<h1>Welcome, {contextData.user.name}</h1>
{contextData.permissions.includes('admin') && (
<button>Admin Panel</button>
)}
</div>
);
}
// Use in a framework layout component
function FrameworkLayout() {
const contextData = useFrameworkContext<AppContextData>();
return (
<header>
Current theme: {contextData.theme}
User: {contextData.user.name}
</header>
);
}The framework application can provide context data that child applications can access:
FrameworkContext.ProvideruseFrameworkContext() to read the dataThe context is automatically provided by the child runtime:
// Automatically generated by child runtime
<FrameworkContext.Provider value={{ ...customProps }}>
{childApplication}
</FrameworkContext.Provider>Where customProps comes from the LifecycleOptions.customProps passed during mount.
Context data flows through this chain:
LifecycleOptions.customPropsuseFrameworkContext() to read the dataComplete example showing framework-to-child communication:
// Framework application (src/app.ts)
import { defineFrameworkConfig } from '@ice/plugin-icestark/types';
export const icestark = defineFrameworkConfig({
getApps: () => [
{
name: 'child-app',
activePath: '/child',
container: '#child-container',
url: ['//localhost:3001/build/js/index.js'],
// Custom properties passed to child
props: {
theme: 'dark',
apiBaseUrl: 'https://api.example.com',
permissions: ['read', 'write'],
},
},
],
});
// Child application component
import { useFrameworkContext } from '@ice/plugin-icestark/Context';
interface FrameworkProps {
theme: string;
apiBaseUrl: string;
permissions: string[];
}
function ChildApp() {
const { theme, apiBaseUrl, permissions } = useFrameworkContext<FrameworkProps>();
// Use framework-provided data
const apiClient = new ApiClient(apiBaseUrl);
return (
<div data-theme={theme}>
<h1>Child Application</h1>
{permissions.includes('write') && (
<button>Create New Item</button>
)}
</div>
);
}The useFrameworkContext hook provides compile-time type safety:
For dynamic communication between framework and child apps, consider: