Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
contextism
Advanced tools
Contextism 🤩 is a new way to use React Context better.
Read this article to become familiar with the idea.
npm i contextism
// or
yarn add contextism
We have two ways to use Contextism, Creating store using it or using its hooks directly:
// store.js
import { createStore } from 'contextism';
const context = createStore("default value for state");
export const { Provider, useDispatchContext, useStateContext, useStore } = context;
// App.jsx
import Div from './Div'
import { Provider } from './store';
const App = () => {
const [ state, dispatch ] = React.useState("Value for state"); // or useReducer
return (
<Provider state={state} dispatch={dispatch}>
// Components you want to use the state there.
<Div />
</Provider>
)
}
// Div.jsx
import { useStateContext, useDispatchContext, useStore } from './store';
const Div = () => {
const state = useStateContext(); // "Value for state"
const dispatch = useDispatchContext(); // dispatch function (setState) in App
// or better one
const [state, dispatch] = useStore();
return ...
}
When we create store using Contextism, it gives us 3 hooks :
useStateContext: the state value that we gave it to state prop in Provider component
useDispatchContext: the setState function or useReducer dispatch that we passed it to dispatch prop
useStore: returns us an array with two values of the above hooks; [ useStateContext, useDispatchContext ]
NOTE: you should use these hooks( methods of createStore function) in child components of Provider component.
Contextism has two hooks beside createStore function:
// Store.jsx
export const CountStateContext = React.createContext();
export const CountDispatchContext = React.createContext();
function countReducer(state, action) {
...
}
export function CountProvider({children}) {
const [state, dispatch] = React.useReducer(countReducer, {count: 0});
return (
<CountStateContext.Provider value={state}>
<CountDispatchContext.Provider value={dispatch}>
{children}
</CountDispatchContext.Provider>
</CountStateContext.Provider>
)
}
// App.jsx
import { CountProvider } from './Store';
import Div from './Div';
export function App() {
return (
<CountProvider>
<Div />
</CountProvider>
)
}
// Div.jsx
import { CountStateContext, CountDispatchContext } from './Store';
import { useContext, useStore} from 'contextism';
export function Div() {
const state = useContext(CountStateContext);
const dispatch = useContext(CountDispatchContext);
// Or much better:
const [state, dispatch] = useStore(CountStateContext,CountDispatchContext);
return ...
}
Contextism has Typescript support like generics and ... . in createStore you can pass two generics too, first one for the state structure and interface, the second one for the useReducer hook.
type Action = {type: 'increment'} | {type: 'decrement'}
type State = { count: number }
// The second generic is for useReducer Action
const context = createStore<State, Action>();
// For useState just pass the first generic (State interface generic)
const context = createStore<State>();
I'm developer, not a perfect person, so I make much mistakes, it means that be free to create issues and then PRs.
Special thanks for contributing and using this project.
FAQs
New way to use React Context
We found that contextism 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
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.