YMapContext
The YMapContext 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.
YMapContext doesn't contain any information. It determines which context is read or provided by other components. The context provider is the YMapContextProvider class.
YMapContextProvider
YMapContextProvider is a context provider for YMap, 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 YMapContextProvider({context: SomeMapContext, value: {your: 'value'}});
map.addChild(mapContextProvider);
And for determining the consumer context:
class SomeContextConsumer extends ymaps3.YMapEntity<{}> {
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:
<YMapContextProvider context={SomeMapContext} value={{your: 'value'}}>
<YMapContainer>
<YMapContainer>
<SomeContextConsumer />
<YMapContainer>
</YMapContainer>
</YMapContextProvider>
Constructor
new YMapContextProvider<T>(props, options?)
Constructor parameters
|
Parameter |
Type |
Description |
|
|
Value of input |
|
|
|
Optional object parameters. |
Inherited from YMapGroupEntity.constructor.
new YMapContextProvider<T>(props, children?, options?)
Constructor parameters
|
Parameter |
Type |
|
|
|
|
|
|
|
|
|
Inherited from YMapGroupEntity.constructor.
Props
YMapContextProviderProps<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?): YMapContextProvider<T>
Parameters
|
Parameter |
Type |
|
|
|
|
|
|
Returns
Inherited from
removeChild
removeChild(child): YMapContextProvider<T>
Parameters
|
Parameter |
Type |
|
|
|
Returns
Inherited from
update
update(changedProps): void
Method for updating props of Entity.
Parameters
|
Parameter |
Type |
Description |
|
|
New |
Returns
void