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 |
|
|
Value of input |
|
|
|
Optional object parameters. |
Inherited from MMapGroupEntity.constructor.
new MMapContextProvider<T>(props, children?, options?)
Constructor parameters
|
Parameter |
Type |
|
|
|
|
|
|
|
|
|
Inherited from MMapGroupEntity.constructor.
Props
MMapContextProviderProps<T>: Object
Parameters
|
Parameter |
Type |
Description |
|
|
|
Context that will receive the provided value. |
|
|
|
Value that must be specified in the context. |
Methods
addChild
addChild(child, index?): MMapContextProvider<T>
Parameters
|
Parameter |
Type |
|
|
|
|
|
|
Returns
Inherited from
removeChild
removeChild(child): MMapContextProvider<T>
Parameters
|
Parameter |
Type |
|
|
|
Returns
Inherited from
update
update(changedProps): void
Method for updating props of Entity.
Parameters
|
Parameter |
Type |
Description |
|
|
New |
Returns
void