
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
react-context-mutation
Advanced tools
to replace redux,Provider value contain useActions which is to merge context state value and useActions return an immutable function collection called `actions`
English | 中文
react-context-mutation is a lighter and more convenient state manager designed for react applications. It aims to replace the Redux in react applications and solve the problems of only one Store in Redux and Non Pluggable state maintenance in the project.
npm install react-context-mutation
// ./App/context.js
import createAppContext from 'react-context-mutation';
import state from './state'; // initial state tree
import configReducer from './config-reducer'; // pre reduce config function
export default createAppContext(state, configReducer)
// ./App/index.js
import AppContext from './context'
import Header from './Header'
const { AppProvider, AppConsumer } = AppContext
export default function App() {
return (
<AppProvider>
<AppConsumer>
{({ context, useActions }) => (
<Layout>
<Header context={context} useActions={useActions} />
<Layout>
<Sider context={context} useActions={useActions} />
<Content>
<Router />
</Content>
</Layout>
</Layout>
)}
</AppConsumer>
</AppProvider>
)
}
// ./App/state.js
export default { // initial state tree
app: {},
header: {},
sider: {},
}
// ./App/config-reducer.js
export default function configReducer({ app = {}, header = {}, sider = {} }) { // pre reduce config function
return (state) => ({
app: mergeAppConfig(app, state.app),
sider: mergeSiderConfig(sider, state.sider),
header: mergeHeaderConfig(header, state.header),
})
}
// ./Header/index.js
import createActions from './actions';
export default function Header(props) {
const { context, useActions } = props;
const { menu, currentItem } = context.header;
const actions = useActions('header', createActions); // `header` is namespace, actions is immutable
const handleMenuChange = useCallback((currentItem) => {
actions.changeCurrent(currentItem)
}, [actions]); // actions is immutable
return (
<header>
<Menu menu={menu} currentItem={currentItem} onMenuChange={handleMenuChange} />
</header>
)
}
// ./Header/actions.js
export default (mutation, contextRef) => ({ // `mutation`and`contextRef` from closure
changeCurrent(currentItem) {
// you can fetch data hear
mutation.header(() => ({ currentItem }));
// await for fetch has done, mutation the header context value then
}
})
exportexport default is a factory function, receive state tree and a predicted reducer function, return <AppProvider> and <AppConsumer>.
createAppContext(state[, configReducer])
The provider receives a config attribute and make the custom config merge to the abstraction of business framework.
<AppProvider config={/* initial config inner state */}>...</AppProvider>
The consumer component can subscribe to the change of context. This component allows you to subscribe to context in functional components. useActions return an immutable collection of actions used to change context value.
<AppConsumer>
{({ context, useActions, mutation }) => /* regular context to use */}
</AppConsumer>
Context provides a way to share such values among components without explicitly passing props layer by layer through the component tree, in order to share the data that is "global" to a component tree.
<AppConsumer>
{({ context }) => // `context` contains the whole tree of state
<Header context={context} />
}}
</AppConsumer>
function Header(props) {
const { context } = props
const { menu } = context.header // get `header` namespace
return (
<header>
<Menu menu={menu} />
</header>
)
}
useActions is used to obtain the changes of update status in functional components. Accept a key of namespace and a closure funtion that provide mutation and contextRef.
const { useActions } = props
const actions = useActions(namespace, createActions)
Mutation is used to update the status of components.
const { mutation } = props
mutation[namespace](reducer)
FAQs
to replace redux,Provider value contain useActions which is to merge context state value and useActions return an immutable function collection called `actions`
We found that react-context-mutation demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers 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
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.