Link Search Menu Expand Document

MessageContext variable

The context object used internally by the library. Probably only useful with Class.contextType or for building custom hooks.

Signature:

MessageContext: Context<MessageContext>

Example

import React, { Component } from 'react'
import {
  getMessage,
  getMessageGetter,
  MessageContext,
  MessageProvider
} from '@messageformat/react'

const messages = {
  example: { key: 'Your message here' },
  other: { key: 'Another message' }
}

class Example extends Component {
  render() {
    const message = getMessage(this.context, 'example.key')
    const otherMsg = getMessageGetter(this.context, 'other')
    return (
      <span>
        {message} | {otherMsg('key')}
      </span>
    ) // 'Your message here | Another message'
  }
}
Example.contextType = MessageContext

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