Combining/Blending - Tagged Reducers/Mappers
Consumption
Reducer or Mapper will never be accessible directly from children elements, they will be able to access the State and/or Dispatcher.
There are different ways of doing this:
injectTaggedReducer, which give access both State andDispatcher.injectTaggedReducerDispatcher, which give access only theDispatcher.injectTaggedReducerState, which give access only the State.
or
injectTaggedMapper, which give access both State andDispatcher.injectTaggedMapperDispatcher, which give access only theDispatcher.injectTaggedMapperState, which give access only the State.
or
- injectTaggedAny, which give access any tagged State and
Dispatcher.
When using any
injectTagged*, , Be Aware that they useReact Consumer, a Consumer component will always re-render when the context value changes, in this case whenstatechanges, therefore when usinginjectReducerDispatcher/injectMapperDispatcheralthough it not depends “directly” onstatethe component will be re-render whenstatechanges. Final words, useSyncMapperProviderand/orAsyncMapperProvider,SyncReducerProviderand/orAsyncReducerProvidereverywhere is required and useinjectReducer/injectMapper,injectReducerDispatcher/injectMapperDispatcherand/orinjectReducerState/injectMapperStatewisely (small scopes, as close to where is required with small amount of children).
injectTaggedReducer/injectTaggedMapper
injectTaggedReducer(ComponentClass, injectedPropName, tag, id)
injectTaggedMapper(ComponentClass, injectedPropName, tag, id)
parameters:
ComponentClass: class: Component class to be enhanced withreact-reducer-providerproperties.injectedPropName: string: Desired name of the property to be injected that correspond to the reducer/mapper (Be sure to avoid collision).tag: string | number | symbol: that identifies an actions/reducer/state combination.id?: string | number | symbol: constitutes the identifier of the*TaggedProviderbeing accessed.
:exclamation: No Error checking is done behind the scene for Tag, so “keep track” of tags (to avoid getting a
undefinederror).
returns:
-
Enhanced Component Class with the indicated property, which holds a tuple containing:
[0]: thestate.[1]: thedispatcher.[2]: the provider id.[3]: respective provider tag.state: thestate.dispatch: thedispatcher.provider: the provider id.tag: respective provider tag.
Trying to reassign
state,dispatch,provider,tag,[0],[1],[2]or[3]will result in aTypeError: Cannot assign to read only property '..' of object '[object Array]'Exception.
Trying to add new fields will result in aTypeError: can't define property "..": Array is not extensibleException.
For purpose of avoiding re-renders and/or improving performance always use the elements of the tuple as reference, never the tuple perse, keep in mind that the tuple that is returned may change but elements will only change when state changes. This is not an “issue” when using the elements of the tuple as reference or when usinginject*Dispatcherorinject*State. Accessing Specific Tagged Reducer/Mapper:
import { injectTaggedMapper } from 'react-reducer-provider'
import React from 'react'
class ClassComponentA11 extends React.Component {
render() {
const [ state, dispatch, tag ] = this.props.mapper
return (
<button onClick={() => dispatch('ACTION1')}>
Click{state}
</button>
)
}
}
const ClassComponent11 = injectTaggedMapper(ClassComponentA11, 'mapper', 'Tag1', 497)
or
import { injectTaggedMapper } from 'react-reducer-provider'
import React from 'react'
class ClassComponentA11 extends React.Component {
render() {
const { state, dispatch, tag } = this.props.mapper
return (
<button onClick={() => dispatch('ACTION1')}>
Click{state}
</button>
)
}
}
const ClassComponent11 = injectTaggedMapper(ClassComponentA11, 'mapper', 'Tag1', 497)
Accessing Singleton Tagged Reducer/Mapper:
import { injectTaggedReducer } from 'react-reducer-provider'
import React from 'react'
class ClassComponentA1 extends React.Component {
render() {
const [ state, dispatch, tag ] = this.props.reducer
return (
<button onClick={() => dispatch('ACTION1')}>
Click{state}
</button>
)
}
}
const ClassComponent11 = injectTaggedReducer(ClassComponentA11, 'reducer', 'Tag1')
or
import { injectTaggedMapper } from 'react-reducer-provider'
import React from 'react'
class ClassComponentA1 extends React.Component {
render() {
const { state, dispatch, tag } = this.props.mapper
return (
<button onClick={() => dispatch('ACTION1')}>
Click{state}
</button>
)
}
}
const ClassComponent11 = injectTaggedMapper(ClassComponentA11, 'mapper', 'Tag1')
injectTaggedAny
injectTaggedAny(ComponentClass, injectedPropName, id)
parameters:
ComponentClass: class: Component class to be enhanced withreact-reducer-providerproperties.injectedPropName: string: Desired name of the property to be injected (Be sure to avoid collision).id?: string | number | symbol: constitutes the identifier of the*TaggedProviderbeing accessed.
returns:
- Enhanced Component Class with the indicated property, which holds a function
getto obtain the any provider value.
Accessing Specific Tagged Reducer/Mapper:
import { injectTaggedAny } from 'react-reducer-provider'
import React from 'react'
class ClassComponentA1 extends React.Component {
render() {
const [ state, dispatch ] = this.props.reducers.get('Tag1')
return (
<button onClick={() => dispatch('ACTION1')}>
Go up (from {state})!
</button>
)
}
}
const ClassComponent11 = injectTaggedAny(ClassComponentA11, 'reducers', 'someTaggedReducerS0')
Accessing Singleton Tagged Reducer/Mapper:
import { injectTaggedAny } from 'react-reducer-provider'
import React from 'react'
class ClassComponentA1 extends React.Component {
render() {
const { state, dispatch } = this.props.reducers.get('Tag1')
return (
<button onClick={() => dispatch('ACTION1')}>
Go up (from {state})!
</button>
)
}
}
const ClassComponent11 = injectTaggedAny(ClassComponentA11, 'reducers')
injectTaggedReducerDispatcher/injectTaggedMapperDispatcher
injectTaggedReducerDispatcher(ComponentClass, injectedPropName, tag, id)
injectTaggedMapperDispatcher(ComponentClass, injectedPropName, tag, id)
parameters:
ComponentClass: class: Component class to be enhanced withreact-reducer-providerproperties.injectedPropName: string: Desired name of the property to be injected that correspond to the dispatcher (Be sure to avoid collision).tag: string | number | symbol: that identifies an actions/reducer/state combination.id?: string | number | symbol: constitutes the identifier of the*TaggedProviderbeing accessed.
:exclamation: No Error checking is done behind the scene for Tag, so “keep track” of tags (to avoid getting a
undefinederror).
returns:
- Enhanced Component Class with the indicated property, which holds the
dispatcherof the respective Reducer/Mapper Provider.
Accessing Specific Tagged Reducer/Mapper:
import { injectTaggedReducerDispatcher } from 'react-reducer-provider'
import React from 'react'
class ClassComponentA11 extends React.Component {
render() {
return (
<button id='F1' onClick={() => this.props.dispatch('ACTION1')}>
Click1
</button>
)
}
}
const ClassComponent11 = injectTaggedReducerDispatcher(ClassComponentA11, 'dispatch', 'Tag1', 'someTaggedReducerS2')
Accessing Singleton Tagged Reducer/Mapper:
import { injectTaggedReducerDispatcher } from 'react-reducer-provider'
import React from 'react'
class ClassComponentA11 extends React.Component {
render() {
return (
<button id='F1' onClick={() => this.props.dispatch('ACTION1')}>
Click1
</button>
)
}
}
const ClassComponent11 = injectTaggedReducerDispatcher(ClassComponentA11, 'dispatch', 'Tag1')
injectTaggedReducerState/injectTaggedMapperState
injectTaggedReducerState(ComponentClass, injectedPropName, tag, id)
injectTaggedMapperState(ComponentClass, injectedPropName, tag, id)
parameters:
ComponentClass: class: Component class to be enhanced withreact-reducer-providerproperties.injectedPropName: string: Desired name of the property to be injected that correspond to the state (Be sure to avoid collision).tag: string | number | symbol: that identifies an actions/reducer/state combination.id?: string | number | symbol: constitutes the identifier of the*TaggedProviderbeing accessed.
:exclamation: No Error checking is done behind the scene for Tag, so “keep track” of tags (to avoid getting a
undefinederror).
returns:
- Enhanced Component Class with the indicated property, which holds the
stateof the respective Reducer/Mapper Provider.
Accessing Specific Tagged Reducer/Mapper:
import { injectTaggedReducerState } from 'react-reducer-provider'
import React from 'react'
class ClassComponentAN2 extends React.Component {
render() {
return (
<div>
ChildN{this.props.state}
</div>
)
}
}
const ClassComponentN2 = injectTaggedReducerState(ClassComponentAN2, 'state', 'TagN', 'someTaggedReducerS6')
Accessing Singleton Tagged Reducer/Mapper:
import { injectTaggedReducerState } from 'react-reducer-provider'
import React from 'react'
class ClassComponentAN2 extends React.Component {
render() {
return (
<div>
ChildN{this.props.state}
</div>
)
}
}
const ClassComponentN2 = injectTaggedReducerState(ClassComponentAN2, 'state', 'TagN')
More Documentation
AsyncTaggedReducerProvider·SyncTaggedReducerProvider·AsyncTaggedMapperProvider·SyncTaggedMapperProvider.useTaggedAny·useTaggedReducer·useTaggedReducerState·useTaggedReducerDispatcher·useTaggedMapper·useTaggedMapperState·useTaggedMapperDispatcher.AsyncReducerProvider,SyncReducerProvider,AsyncMapperProvider&SyncMapperProvider.useReducer,useReducerState,useReducerDispatcher,useMapper,useMapperState&useMapperDispatcherinjectReducer·injectReducerState·injectReducerDispatcher·injectMapper·injectMapperState·injectMapperDispatcher.- Singleton.
- Nesting Providers.
- Typings.
- With Injection.
- With Actions Creators.
- Testing.
- Examples from tests.
- Typings’ examples from tests.
- Extending/Developing.