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.

useContext or similar global shared state system

See original GitHub issue

i’ve seen a few mention on it in the different Pr/issues but can’t find anything about it in the current fre package. Is it still something that is planned? A Pr was also talking about a fre/compat for context. Is this a future plan or already there(couldn’t find it)?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
yisarcommented, Jun 9, 2021

Because of the upgrade of new version fre2, this work was not performed, but this is what we will do in the future.

Shared state may be easy with hooks API, for example:

export const createContext = defaultValue => {
  const context = {
    value: defaultValue,
    subs: new Set(),
    Provider: function Provider({ value, children }) {
      useLayout(() => {
        context.subs.forEach(fn => fn(value))
        context.value = value
      })
      return children
    }
  }
  return context
}

export const useContext = (context, selector) => {
  const subs = context.subs
  const [, forceUpdate] = useReducer(c => c + 1, 0)
  const selected = selector ? selector(context.value) : context.value
  const ref = useRef(null)
  useEffect(() => {
    ref.current = selected
  })
  useEffect(() => {
    const fn = nextValue => {
      if (ref.current === selector(nextValue)) return
      forceUpdate()
    }
    subs.add(fn)
    return () => subs.delete(fn)
  }, [subs])
  return selected
}

const Context = createContext({ count1: 0, count2: 1 })

function App(){
  const context = useContext(Context, ctx => ctx.count1) // selector
...
}

After fre2 is finally released, we can add some api and compat packages, it will be soon.

0reactions
yisarcommented, Sep 1, 2021

@mindplay-dk I don’t want to use pub-sub in the core code of fre (just like preact). If I have to, I prefer to set shoudComponentUpdate for all components, and then use pub-sub similar technologies

Read more comments on GitHub >

github_iconTop Results From Across the Web

useContext for better state management! - DEV Community ‍ ‍
React context is nothing but a global state to the app. It is a way to make a particular data available to all...
Read more >
How To Share State Across React Components with Context
In this tutorial, you'll share state across multiple components using React context. React context is an interface for sharing information ...
Read more >
How to share state across React Components with context
In a small app, React Context along with useState is the best way to share state or simply pass data around components without...
Read more >
Share Your Data With Context and useContext
React's documentation states that “Context is designed to share data that can be considered global,” for example, current authenticated user ...
Read more >
Using React's Context API for Global State Management
To put it simply, global state is the data that is shared between all the components within a React application. When the state...
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