React Hooks in the Component SDK
Author:
Fluent Commerce
Changed on:
15 July 2024
Overview
Component SDK provides a collection of React hooks to allow custom components to access the core UX framework functionality.The SDK includes Typescript declaration files that tell your IDE and compiler what these look like and that they are available on the global scope.Key points
- Component SDK provides a collection of React hooks to allow custom components to access the core UX framework functionality.
- The useAuth can access user session information.
- The useEnv can access the current environment details.
- The useI18n can access the global language to translate static labels
- The useSetting / getSettings can access the current account setting values.
- The useQuery/getQuery and useRest/getRest to retrieve data from OMS.
- The useData hook provides direct access to the page query response and variables.
Hooks
Component SDK provides a collection of React hooks to allow custom components to access the core UX framework functionality.The SDK includes Typescript declaration files that tell your IDE and compiler what these look like and that they are available on the global scope.useAuth
User login session information, provides access to:- details of the logged in user
- current roles and permissions
- ability to switch contexts (i.e. between different retailers or locations the user has access to)
- ability to log out
useEnv
Details of the current environment:- the Fluent Account name
- the current Web App name (eg. "oms")
useI18n
Automatically translate static labels, generally for things that aren't coming from manifest configuration (as those are automatically translated).If the user changes languages during a session, these strings will automatically be re-translated and the component will re-render without requiring additional code.For example, on an ID confirmation component:In some cases you might find yourself with several keys that might fit the label, or want to provide a default value in the code. The`translateOr` function allows several keys to be passed in, and will return either the value of the first language-file match, or the literal value of the last key (as a default).Finally, the useI18n hook provides information about the available and currently selected languages.useSettings
Many components are configured at a retailer or account level using settings. The useSetting hook gives easy access to those values.By default,`useSettings` will find the setting value at the current context the user is logged into. Depending on the web app manifest setup this could be a location or a retailer.From there, it will cascade up the context hierarchy (location, to retailer, to account) until it finds a setting of the name provided and return the first (meaning most specific) match.getSettings
For cases where a hook is not a good fit, a Promise-based variant of`useSettings` is provided in `mystique/hooks/getSettings`.An example can be found in hereuseQuery and useRest
For most cases we strongly encouraged letting the UX framework build and run the query based on the manifest configuration, for performance and complexity reasons, but there are cases in which a single query is not enough to gather all of the data required for a given page. This is especially true for cases where the result of one query is needed to build the follow-up query.The useQuery is available to components for those edge cases.The useRest example can be found here.A simple useQuery Example
useQuery Example with useAuth() as query variable
Example of useQuery to handle 'edges' nodes in the result.
Example of useQuery using contextEntity as input variable
Example of useQuery and use Map function to display the data
Example of useQuery returning a single result
getQuery and getRest
For cases where a hook is not a good fit, a Promise-based variant of each is provided in`mystique/hooks/getQuery` and `mystique/hooks/getRest` respectively.An example of `getQuery` can be found heregetApiDownload
There are a few Fluent REST API endpoints that produce a file.The getApiDownload hook (module`mystique/hooks/getApiDownload`) allows a component to retrieve this data in the form of a blob, which can then either be rendered or downloaded as required.useData
The useData hook provides direct access to the page query response and variables.It can be used to build components that alter the page query in response to user interaction, like the list filter.useUserActions and useUserActionForm
User actions are used to indicate in the workflow that a ruleset should appear in the UI. This usually take the form of a button which, if required, presents a modal to capture any extra information needed to process the action.The useUserActions hook allows an SDK developer to get a list of the available user actions on any entity returned by the page query.By default, it will return user actions for the first entity found at the current dataSource root (i.e. the`data` object passed into this component), but this can be changed using the `path` parameter (in the form of a `JSONPath` relative to the current dataSource root).The useUserActionForm generates a form for a named user action. The target entity is chosen using the same logic as the useUserActions hook.