MMapContext

The MMapContext class creates a context that components can provide or read.

The most common way to transfer information from a parent component to a child component is via props. However, if a tree has a complex structure of components or multiple components need the same information, transferring props can become cumbersome and inconvenient. Context enables a parent component to make some information available to any component in the tree below it without explicitly transferring it via props.

MMapContext doesn't contain any information. It determines which context is read or provided by other components. The context provider is the MMapContextProvider class.

MMapContextProvider

MMapContextProvider is a context provider for MMap, allowing the input of the context and its value.

Usage example

A content provider for Map for inputting the context and its value:

const mapContextProvider = new MMapContextProvider({context: SomeMapContext, value: {your: 'value'}});
map.addChild(mapContextProvider);

And for determining the consumer context:

class SomeContextConsumer extends mappable.MMapEntity<{}> {
   constructor() {
       super({});
   }

   _onAttach() {
     const value = this._consumeContext(SomeMapContext);
     console.log(value); // {your: 'value'}
   }
}

When nested containers are added, the context is available in them:

mapContextProvider.addChild(new SomeContextConsumer());

But most importantly, it can be passed at any nesting level:

<MMapContextProvider context={SomeMapContext} value={{your: 'value'}}>
    <MMapContainer>
        <MMapContainer>
            <SomeContextConsumer />
        <MMapContainer>
    </MMapContainer>
</MMapContextProvider>

Constructor

new MMapContextProvider<T>(props, options?)

Constructor parameters

Parameter

Type

Description

props

MMapContextProviderProps<T>

Value of input props.

options?

ComplexOptions<MMap>

Optional object parameters.

Inherited from MMapGroupEntity.constructor.

new MMapContextProvider<T>(props, children?, options?)

Constructor parameters

Parameter

Type

props

MMapContextProviderProps<T>

children?

GenericEntity<unknown, {}, MMap>[]

options?

Omit<ComplexOptions<MMap>, "children">

Inherited from MMapGroupEntity.constructor.

Props

MMapContextProviderProps<T>: Object

Parameters

Parameter

Type

Description

context

MMapContext<T>

Context that will receive the provided value.

value

T

Value that must be specified in the context.

Methods

addChild

addChild(child, index?): MMapContextProvider<T>

Parameters

Parameter

Type

child

MMapEntity<unknown, {}>

index?

number

Returns

MMapContextProvider<T>

Inherited from

MMapGroupEntity.addChild

removeChild

removeChild(child): MMapContextProvider<T>

Parameters

Parameter

Type

child

MMapEntity<unknown, {}>

Returns

MMapContextProvider<T>

Inherited from

MMapGroupEntity.removeChild

update

update(changedProps): void

Method for updating props of Entity.

Parameters

Parameter

Type

Description

changedProps

Partial<MMapContextProviderProps<T>>

New props values.

Returns

void

Inherited from

MMapGroupEntity.update