
Research
/Security News
Popular Tinycolor npm Package Compromised in Supply Chain Attack Affecting 40+ Packages
Malicious update to @ctrl/tinycolor on npm is part of a supply-chain attack hitting 40+ packages across maintainers
react-context-simply
Advanced tools
leveraging the react context api for state management with no boilerplate code
A small package that will helps you manage your app state using react context api and hooks
npm install react-context-simply --save
constanst.js
// file holds all our constants will be used in dispatching actions also in reducer
export const INCREMENT = 'INCREMENT';
export const DECREMENT = 'DECREMENT';
actions.js
import { INCREMENT, DECREMENT } from "./constants";
export const increment = () => ({ type: INCREMENT });
export const decrement = () => ({ type: DECREMENT });
reducers.js
import { INCREMENT, DECREMENT } from "./constants";
export default function countReducer(state, action) {
switch (action.type){
case INCREMENT:
return state + 1;
case DECREMENT:
return state - 1;
default:
return state;
}
}
store.js
import createStateContext from 'react-context-simply';
import countReducer from './reducers';
import * as actions from './actions'
const {
useStateValue,
StateProvider,
StateContext
} = createStateContext({}, countReducer, actions);
const useCountState = useStateValue;
const CountState = StateProvider;
const CountStateContext = StateContext
export {
useCountState,
CountState,
CountStateContext
};
app.js
import React from 'react';
import { CountState, useCountState } from './store.js';
function Counter() {
const [state, actions] = useCountState()
const {increment, decrement} = actions
return (
<View style={{flex: 1, flexDirection: 'column', alignItems: 'center', justifyContent: 'center'}}>
<TouchableOpacity onPress={()=>{
increment()
}}>
<Text>Increment</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{
decrement()
}}>
<Text>Decrement</Text>
</TouchableOpacity>
<Text>{state}</Text>
</View>
);
}
export default class App extends React.Component {
render() {
return (
<CountState>
<Counter />
</CountState>
);
}
}
for me i prefer to seperate the code into files but you can do it in one file, its all up to you
the consumer component it should be a functional component but its possible to acces state in class based component like this:
```
import React, { Component } from 'react';
import { CountStateContext } from './store';
class Counter extends Component {
static contextType = CountStateContext;
render() {
const [state, actions] = this.context;
}
}
```
FAQs
leveraging the react context api for state management with no boilerplate code
We found that react-context-simply 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.
Research
/Security News
Malicious update to @ctrl/tinycolor on npm is part of a supply-chain attack hitting 40+ packages across maintainers
Security News
pnpm's new minimumReleaseAge setting delays package updates to prevent supply chain attacks, with other tools like Taze and NCU following suit.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.