r/reactjs • u/TheHganavak • May 13 '26
Resource Pocket: One-call factory that collapses React Context boilerplate from ~20 lines to 1
https://github.com/icydotdev/pocket
41
Upvotes
r/reactjs • u/TheHganavak • May 13 '26
76
u/domlebo70 May 13 '26
Very cool/useful. I'll just copy the 30 lines of code rather than add a supply chain vuln potential though
32
u/tyqe May 13 '26
makes me think there wouldn't be so much confusion around state management if the React team had just created a simple interface for context like this in the first place.
I respect the idea!
12
u/TheHganavak May 13 '26
Hey React folks,
I got tired of writing the same
Contextboilerplate every time —createContext+ aProvidercomponent +useContext+ a manual undefinedthrow— especially with TypeScript on top. ~20 lines per context, every single time.So I made a tiny npm package that wraps it up. One call, one line:
That's a typed
Provider+ aconsumerhook. Drop theProvideranywhere, destructure[theme, setTheme]in any descendant — exactly likeuseState.It sits pretty close to libraries like constate, Jotai, Zustand — but unlike those, it doesn't add anything extra. No selectors, no atoms, no store. It's literally just React Context with the boilerplate removed. Same render semantics, same SSR story. If you know Context, you know how this behaves.
Repo: https://github.com/icydotdev/pocket
npm: @icydotdev/pocket
Roast it.
2
u/acraswell May 13 '26
This is great for single value providers. For slightly more complex state where you might want multiple values or even functions that change value, is there a pattern for that?
2
u/NotZeldaLive May 14 '26
Honestly really good. Just reduced the boiler plate super well but kept simple. Kinda upset I didn't come up with this initially.
This solves a problem for me that context felt very verbose, but state management like Zustand can't handle initial values being passed in from props without a null render pass.
Doubt you will get many installs though when the pattern can be copied directly without adding another dep, so you have my thanks directly.
1
u/frogic May 14 '26
Doesn’t your example break the rules of hooks? I know it mostly doesn’t matter but you might get into some fistfights with the react compilerÂ
1
u/barkwahlberg May 14 '26
Thanks, Claude
1
u/t3hlazy1 May 14 '26
Your first image is annoying. You say 22 lines -> 1 line. The image shows 21 lines -> 4 lines or 5 lines with wrapping.
0
u/sebastienlorber May 18 '26
This reminds me of another similar solution: https://github.com/nkzw-tech/create-context-hook
Apart from little differences in managing the defautl value and provide displayName, it's quite the same
-2
u/Cahnis May 14 '26
An AI skill would be a better solution and you get to own the code instead of owning an abstraction