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.

Subscribe to state changes outside of React

See original GitHub issue

Hey @RichardLindhout! I am still very happy with this tiny but powerful library 😃

I’d like to listen for state changes to trigger side effects (e.g. logging, tracking). For now I need to this in store.set itself, but I’d like to have this side effect free.

Maybe something along those lines:

const unsubscribe = myRidgeStore.subscribe((prevState, newState) => {
  // side effects here
})

Unfortunately onSet doesn’t suffice as it happens after the state already has changed, I would also be fine with another option at store creation. But in my opinion the approach I suggested is a little bit more approachable.

Thanks 😃

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
RichardLindhoutcommented, Dec 30, 2020

Will try to add within two weeks

1reaction
RichardLindhoutcommented, Jan 28, 2021

And possible unsubscribing?

const unsubscribe = myRidgeStore.subscribe((new) => {}
unsubscribe()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Update component state from outside React (on server ...
My main question is, is it possible to trigger a component state change from outside react (in this case on server response)?
Read more >
How to subscribe to state outside React component in Redux ...
Coding example for the question How to subscribe to state outside React component in Redux Toolkit?-Reactjs.
Read more >
Access the Redux Store Outside a React Component
Access the Redux Store Outside a React Component · Option 1: Export the Store · Option 2: Access Redux State from a Thunk...
Read more >
Context - React
A React component that subscribes to context changes. ... inside the ThemeProvider // uses the theme from state while the one outside uses...
Read more >
Redux Fundamentals, Part 5: UI and React
Those UI binding libraries handle the details of subscribing to the store and efficiently updating the UI as state changes, so that you ......
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