question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Extract the logic that makes hook shareable without Provider/Context

See original GitHub issue

I’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:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremyRHcommented, Apr 25, 2019

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 and dispatch functions in a list and calling them when needed. It is possible for someone to create a package that does this though:

import { useReducer, useState } from 'react';
import share from 'react-share-state';
export const useSharedReducer = share(useReducer);
export const useSharedState = share(useState);
1reaction
timkindbergcommented, Apr 16, 2019

I’m thinking more if someone already had a hook written.

function useMyState() {
  const [state, dispatch] = userReducer(...)
  const actions = {...}
  // maybe some more logic
  return someApi
}

Then they could just make it “shared” state, meaning it could be used in multiple components. Like a higher-order hook.

export useMySharedState = share(useMyState)

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found