Hooks
The @akinon/app-client library provides a set of React hooks that allow you to
easily access and utilize the functionality provided by the app client context.
useAppClient
The useAppClient hook is the primary way to access the app client context and
its associated functions within your components.
Usage
import { useAppClient } from '@akinon/app-client';
const MyComponent = () => {
const {
data,
params,
isLoading,
invokeAction,
navigate,
showModalDialog,
showConfirmationDialog,
showToast,
showErrorMessage,
showRichModal
} = useAppClient();
// Use the context values and functions in your component
};Return Value
The useAppClient hook returns an object with the following properties and
methods:
| Name | Type | Description |
|---|---|---|
data | ApplicationData | undefined | Shared application data |
modalContext | unknown | Rich modal context data |
params | ApplicationParams | undefined | Additional parameters passed to the application |
isLoading | boolean | Indicates whether the application data is currently loading |
invokeAction | <T = any>(actionKey: string, ...args: any[]) => Promise<T> | Method to invoke a custom action defined in the app shell |
navigate | (payload: ShellNavigationPayload) => void | Method to navigate to a different route |
showModalDialog | (title: string, content: string) => void | Display a modal dialog |
showConfirmationDialog | (title: string, content: string) => boolean | Display a confirmation dialog |
showToast | (content: string, type: 'success' | 'warning' | 'error' | 'loading' | 'destroy') => void | Display a toast notification |
showErrorMessage | (title: string, content: string) => void | Display an error message dialog |
showRichModal | (path: string, context?: unknown) => void | Display a rich content modal |
Example
import React from 'react';
import { useAppClient } from '@akinon/app-client';
const NavigationButton = () => {
const { navigate } = useAppClient();
return (
<button onClick={() => navigate({ path: '/dashboard' })}>
Go to Dashboard
</button>
);
};
const ActionButton = () => {
const { invokeAction } = useAppClient();
const handleClick = async () => {
try {
const result = await invokeAction('customAction', 'arg1', 'arg2');
console.log('Action result:', result);
} catch (error) {
console.error('Action failed:', error);
}
};
return <button onClick={handleClick}>Invoke Custom Action</button>;
};By using the useAppClient hook, you can easily access and utilize the various
functionalities provided by the app client context throughout your application.