useMessageGetter() function

A custom [React hook] providing a message getter function, which may have a preset root id path, locale, and/or base parameters for message functions.

The returned function takes two parameters (msgId, msgParams), which will extend any values set by the hook’s arguments.

Signature:

export declare function useMessageGetter(rootId: string | string[], opt?: MessageGetterOptions): (id?: string | string[] | undefined, params?: Record<string, unknown> | undefined) => any;

Parameters

Parameter Type Description
rootId string | string[] The key or key path of the message or message object. If empty or [], matches the root of the messages object
opt MessageGetterOptions (Optional)

Returns:

(id?: string | string[] | undefined, params?: Record<string, unknown> | undefined) => any

Example

import React from 'react'
import { MessageProvider, useMessageGetter } from '@messageformat/react'

const messages = {
  example: {
    funMsg: ({ thing }) => `Your ${thing} here`,
    thing: 'message'
  }
}

function Example() {
  const getMsg = useMessageGetter('example')
  const thing = getMsg('thing') // 'message'
  return getMsg('funMsg', { thing }) // 'Your message here'
}

export const App = () => (
  <MessageProvider messages={messages}>
    <Example />
  </MessageProvider>
)