Extract the logic that makes hook shareable without Provider/Context
See original GitHub issueI’m loving the look of this lib! I’m currently rewriting some of my state management as a POC using it.
I was curious, would you be able to extract the logic that makes this hook “shareable” in any component without needing a Provider? If so that extracted utility would be highly valuable to the hook community. Any hook could use this utility to convert the regular hook to a “works anywhere as a singleton” hook.
Or is the “sharing” capability too coupled with the rest of the library?
Thinking something like this:
// This hook could now be used in many places with the same reference state.
const useSharedState = share(() => {
const [state, setState] = useState(initialState)
return [state, setState]
})
Some prior art are:
- constate: https://github.com/diegohaz/constate (but still uses Provider)
- hookleton: https://github.com/bySabi/hookleton (seems to achieve what I’m talking)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Extract the logic that makes hook shareable without Provider ...
A way to share state with many components without using Provider; An elegant state API. 1 is a worthwhile discovery on its own....
Read more >Hook your component — React - codeburst
Hooks is a feature, which helps developer to manage or hold the logic of the state inside functional component. In React, we write...
Read more >Handling Authentication in React with Context and Hooks
With Context, it becomes easier to access props in different components, without having to pass them down on each level explicitly.
Read more >Understanding Design Patterns in React
Extracting commonly needed logic in a hook make for clean code ... Provider pattern allows us to store data (global or shareable in...
Read more >Learn Provider Pattern – A Tour of JavaScript & React Patterns
So if you wanna share stateful logic throughout your application using Hooks, you can use the Provider Pattern. And this is kind of...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This would be nice but I don’t know if your example is possible. State is shared without using a provider by collecting the
setState
anddispatch
functions in a list and calling them when needed. It is possible for someone to create a package that does this though:I’m thinking more if someone already had a hook written.
Then they could just make it “shared” state, meaning it could be used in multiple components. Like a higher-order hook.
Anyway, sorry to beat a dead horse 😃 THANK YOU for this library, we’ve started using it in our startup’s site. It’s going well so far!