Link Search Menu Expand Document

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.


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


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  


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


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 />