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.

Auto unsubscribe on the element is detached

See original GitHub issue

Thanks for the library.

Let’s say I have the following example:

function App() {
  const [show, setShow] = React.useState(true);
  const [text, updateText] = useObservableState<string>(
    text$ => text$.pipe(delay(1000), map(() => Math.random().toString()), finalize(() => console.log('done'))),
    'Init'
  )

  return (
    <section>
      <button onClick={() => setShow((v) => !v)}>Toggle</button>
        {show && <h1 onClick={updateText}>{text}</h1>}
    </section>
  )
}

When we toggle the element the subscription is still alive. The finalize isn’t invoked. How do we deal with this situation?

Thanks.

Btw, I also get a type error:

image

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
crimxcommented, Jul 14, 2020

Unfortunately as I had explained in the docs performing synchronous side effects is not safe in concurrent mode.

It might be okay if just a Behaviour Subject but users may create all kinds of observable pipelines. If you have for example an initial remote data fetching, this could be triggered multiple times if the subscription happens in render phase.

0reactions
ArielGuetacommented, Jul 14, 2020

Ok, thanks. Last question:

What about a use case where I have a BehaivorSubject? In this case, I expect that useObservable will take the initial value synchronously and use it instead of passing a “redundant” initial value and cause an additional re-render. What do you think?

It’s common usage in state management libraries such as Akita.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Knockout computed observable is unsubscribed when ...
I'm using jQuery detach which preserves the knockout data on the elements, and then reattaching with attach .
Read more >
Stores / Auto-subscriptions • Svelte Tutorial
Calling a subscribe method returns an unsubscribe function. You now declared unsubscribe , but it still needs to be called, for example through...
Read more >
Solving The Element Is Detached From DOM Error In ...
A video of a page refreshing and breaking a Cypress test and several possible solutions. Many people complain about the dreaded "Element is...
Read more >
6 Ways to Unsubscribe from Observables in Angular
In this post, I'll review the different ways you can unsubscribe from Observables in Angular apps. The Downside to Observable Subscription.
Read more >
Detached SVG elements after a call to paper remove
You received this message because you are subscribed to the Google Groups "JointJS" group. To unsubscribe from this group and stop receiving emails...
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