useAppClient

This hook provides access to the client context, including shared data, actions, navigation, and more.

import { useAppClient } from '@akinon/app-client';

const MyComponent = () => {
  const { data, navigate, showToast } = useAppClient();

  return (
    <div>
      <p>User: {data?.user?.name}</p>
      <button onClick={() => navigate({ path: '/home' })}>Home</button>
      <button onClick={() => showToast('Hello!', 'success')}>Greet</button>
    </div>
  );
};

Return Value

The useAppClient hook returns an AppClientContextState object:

Property
Type
Description

data

ApplicationData | undefined

Shared data from the shell

params

ApplicationParams | undefined

Parameters passed to plugin applications

isLoading

boolean

Whether the application data is loading

modalContext

unknown

Context data for rich modals

locale

string

Current locale from the shell

navigate

(payload) => void

Navigate within the shell

invokeAction

(key, ...args) => Promise

Invoke custom shell actions

showModalDialog

(options) => boolean

Show a modal dialog

showConfirmationDialog

(options) => boolean

Show a confirmation dialog

showToast

(content, type) => boolean

Show a toast notification

showErrorMessage

(title, content) => boolean

Show an error message

showRichModal

(path, context?, size?, closeIconColor?) => boolean

Show a rich modal

removeSearchParams

(keys) => void

Remove URL search params

setSearchParams

(params) => void

Set URL search params

onLocaleChange

(callback) => () => void

Subscribe to locale changes

Accessing Shared Data

The data property contains the shared data from the shell:

Request the shell to navigate to a different route:

Invoking Custom Actions

Call custom actions defined in the shell:

Default Actions

showToast

Display a brief notification:

showConfirmationDialog

Display a confirmation dialog with callbacks:

showErrorMessage

Display an error message dialog:

showRichModal

Open a rich modal with another micro-frontend:

URL Search Params

Manage URL search parameters:

Plugin Parameters

Access parameters passed to plugin applications:

Access context data in rich modals:

Locale Handling

React to locale changes from the shell:

Best Practices

1. Handle Loading State

Always check isLoading before accessing data:

2. Optional Chaining for Data

Use optional chaining when accessing nested data:

3. Error Handling for Actions

Wrap action invocations in try-catch:

Next Steps

  • Configuration - Configure your client application

  • i18n - Internationalization

  • API Reference - Complete API documentation

Last updated

Was this helpful?