
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-reducer-provider
Advanced tools
Asynchronous/Synchronous React Centralized State with Hooks and HOC
through Centralized Reducers/Mappers/Actuators
Flux/Redux made Easy, Simple and Beyond
react-reducer-provider provides a centralized state, managed asynchronously or synchronously through a reducer, mapper or actuator.
1 . Add dependency:
package.json:
when using hooks:
..
"dependencies": {
"react": ">=16.8.0"
"react-reducer-provider": "5.1.0",
..
when using HOC:
..
"dependencies": {
"react": ">=16.0.0"
"react-reducer-provider": "5.1.0",
..
2 . Create the AsyncReducerProvider or SyncReducerProvider component to manage the centralized state:
a . Define the initial state.
b . Define the reducer function.
c . Define the Reducer Provider.
SomeReducerProvider.jsx:
import React from 'react'
import { SyncReducerProvider } from 'react-reducer-provider'
const initialState = 0
function reduce(prevState, action, delta) {
switch (action) {
case 'ACTION1':
return prevState + delta
case 'ACTION2':
return prevState - delta
default:
return prevState
}
}
function SomeReducerProvider({ children }) {
return (
<SyncReducerProvider
reducer={reduce}
initialState={initialState}
>
{children}
</SyncReducerProvider>
)
}
export default SomeReducerProvider
3 . Access the state and the dispatcher of the Provider component using the hooks or HOC provided by 'react-reducer-provider':
useReducer/injectReducer.useReducerDispatcher/injectReducerDispatcher.useReducerState/injectReducerState.SomeComponent1.jsx => using useReducer:
import { useReducer } from 'react-reducer-provider'
import React from 'react'
export default function SomeComponent1() {
const { state, dispatch } = useReducer()
return (
<button onClick={() => dispatch('ACTION1', 2)}>
Go up (from {state})!
</button>
)
}
SomeComponent2.jsx => using injectReducerDispatcher:
import { injectReducerDispatcher } from "react-reducer-provider";
import React from "react";
class SomeComponent2 extends React.Component {
render() {
return (
<button onClick={() => {
const newState = dispatch('ACTION2', 1)
console.info(newState)
}}>
Go down!
</button>
)
}
}
export default injectReducerDispatcher(SomeComponent2, 'dispatch')
SomeComponentN.jsx => using useReducerState:
import { useReducerState } from 'react-reducer-provider'
import React from 'react'
export default function SomeComponentN() {
const currentState = useReducerState()
return (
<div>
Current:{currentState}
</div>
)
}
4 . Wrap components which will consume the SomeReducerProvider component:
SomeContainer.jsx:
import SomeComponent1 from './path/to/SomeComponent1'
import SomeComponent2 from './path/to/SomeComponent2'
import SomeComponentN from './path/to/SomeComponentN'
import SomeReducerProvider from '../path/to/SomeReducerProvider'
import React from 'react'
export default function SomeContainer() {
return (
<SomeReducerProvider>
<SomeComponent1/>
<SomeComponent2/>
<SomeComponentN/>
</SomeReducerProvider>
)
}

This
SyncReducerProviderexample can be checked on line at gmullerb-react-reducer-provider codesandbox:
ThisSyncReducerProviderwith HOC example can be checked on line at gmullerb-react-reducer-provider codesandbox:
AnAsyncReducerProviderexample can be checked on line at gmullerb-react-reducer-provider-async codesandbox:
Examples of use can be looked at basecode-react-ts and test files.
2 . Create the AsyncMapperProvider or SyncMapperProvider component to manage the centralized state:
a . Define the initial state.
b . Define the mapper function.
c . Define the Mapper Provider.
SomeMapperProvider.jsx:
import React from "react";
import { SyncMapperProvider } from "react-reducer-provider";
const initialState = 0;
function map(action) {
switch (action) {
case "ACTION1":
return 1;
case "ACTION2":
return -1;
default:
return 0;
}
}
function SomeMapperProvider({ children }) {
return (
<SyncMapperProvider
id="someNamedMapper"
mapper={map}
initialState={initialState}
>
{children}
</SyncMapperProvider>
);
}
export { SomeMapperProvider };
3 . Access the state and the dispatcher of the Provider component using the hooks or HOC provided by 'react-reducer-provider':
useMapper/injectMapper.useMapperDispatcher/injectMapperDispatcher.useMapperState/injectMapperState.SomeComponent1.jsx => using useMapper:
import { useMapper } from "react-reducer-provider";
import React from "react";
export default function SomeComponent1() {
const [state, dispatch] = useMapper("someNamedMapper");
return (
<button onClick={() => dispatch("ACTION1")}>
Set to 1 (from {state})!
</button>
);
}
SomeComponent2.jsx => using useMapperDispatcher:
import { useMapperDispatcher } from "react-reducer-provider";
import React from "react";
export default function SomeComponent2() {
const dispatch = useMapperDispatcher("someNamedMapper");
return <button onClick={() => dispatch("ACTION2")}>Set to -1!</button>;
}
SomeComponentN.jsx => using injectMapperState:
import { injectMapperState } from "react-reducer-provider";
import React from "react";
class SomeComponentN extends React.Component {
render() {
return <div>Current:{this.props.currentState}</div>;
}
}
export default injectMapperState(SomeComponentN, 'currentState')
4 . Wrap components which will consume the SomeMapperProvider component:
SomeContainer.jsx:
import SomeComponent1 from "./SomeComponent1";
import SomeComponent2 from "./SomeComponent2";
import SomeComponentN from "./SomeComponentN";
import { SomeMapperProvider } from "./SomeMapperProvider";
import React from "react";
export default function SomeContainer() {
return (
<SomeMapperProvider>
<SomeComponent1 />
<SomeComponent2 />
<SomeComponentN />
</SomeMapperProvider>
);
}

An
SyncMapperProviderexample can be checked on line at gmullerb-react-mapper-provider codesandbox:
AnAsyncMapperProviderexample can be checked on line at gmullerb-react-mapper-provider-async codesandbox:
Examples of use can be looked at test files.
2 . Create the ActuatorProvider component to manage a state (or whatever you need, not only state):
a . Define actuator function.
b . Define Actuator Provider.
c. Wrap components which will consume the ActuatorProvider:
SomeContainer.jsx:
import SomeComponent1 from './path/to/SomeComponent1'
import SomeComponent2 from './path/to/SomeComponent2'
import { ActuatorProvider } from 'react-reducer-provider'
import React from 'react'
export function SomeContainer() {
const [state, setState] = React.useState(0)
const actuate = delta => setState(state + delta)
return (
<div>
<ActuatorProvider actuator={actuate}>
<SomeComponent1/>
<SomeComponent2/>
</ActuatorProvider>
Current:{state}
</div>
)
}
3 . Access dispatcher of the Provider component using the hooks or HOC provided by 'react-reducer-provider':
SomeComponent1.jsx => using useActuator:
import { useActuator } from 'react-reducer-provider'
import React from 'react'
export function SomeComponent1() {
const dispatch = useActuator()
return (
<button onClick={() => dispatch(+1)}>
Go up!
</button>
)
}
SomeComponent2.jsx => using injectActuator:
import { injectActuator } from "react-reducer-provider";
import React from "react";
class SomeComponent2 extends React.Component {
render() {
return (
<button
onClick={() => {
const newState = this.props.dispatch(-1);
console.info(newState);
}}
>
Go down!
</button>
);
}
}
export default injectActuator(SomeComponent2, "dispatch");

This
ActuatorProviderexample can be checked on line at gmullerb-react-actuator-provider codesandbox:
Examples of use can be looked at test files.
With the introduction of React Hooks, in some way using Flux library[1] was deprecated, react-reducer-provider looks to give a quick and easy alternative using hooks to implement Flux with reducers.
react-reducer-provider in the final app bundle [3].[1] Not the Flux architecture.
[2] react-redux makes it too complicated.
[3] Check and Compare with other solutions at bundlephobia.com.
react-reducer-provideris the evolution of react-named-reducer (which is a derivation of react-reducer-context).
You define:
or
or
and then you use them through a:
At least ["react": ">=16.0.0"] - React Context => when using HOC, i.e. injectReducer · injectReducerState · injectReducerDispatcher · injectMapper · injectMapperState · injectMapperDispatcher or injectTaggedAny · injectTaggedReducer · injectTaggedReducerState · injectTaggedReducerDispatcher · injectTaggedMapper · injectTaggedMapperState · injectTaggedMapperDispatcher.
["react": ">=16.8.0"] - React Hooks => when using hooks, i.e. useReducer · useReducerState · useReducerDispatcher · useMapper · useMapperState · useMapperDispatcher or useTaggedAny · useTaggedReducer · useTaggedReducerState · useTaggedReducerDispatcher · useTaggedMapper · useTaggedMapperState · useTaggedMapperDispatcher.
react-reducer-provideronly enforces"react": ">=16.0.0"inpackage.jsonis up to you to be set which version you need.
ActuatorProvider.Combining/Blending - Tagged Reducers/Mappers/Actuator.
AsyncTaggedReducerProvider · SyncTaggedReducerProvider · AsyncTaggedMapperProvider · SyncTaggedMapperProvider · TaggedActuatorProvider.useTaggedAny · useTaggedReducer · useTaggedReducerState · useTaggedReducerDispatcher · useTaggedMapper · useTaggedMapperState · useTaggedMapperDispatcher · useTaggedActuator.injectTaggedAny · injectTaggedReducer · injectTaggedReducerState · injectTaggedReducerDispatcher · injectTaggedMapper · injectTaggedMapperState · injectTaggedMapperDispatcher · injectTaggedActuator.Extras:
CHANGELOG: add information of notable changes for each version here, chronologically ordered (Keep a Changelog).
Don't forget:
At life:
At work:
FAQs
Asynchronous/Synchronous React Centralized State with Hooks and HOC
We found that react-reducer-provider demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.