fsprojects / Avalonia.FuncUI

Develop cross-plattform GUI Applications using F# and Avalonia!
https://funcui.avaloniaui.net/
MIT License
981 stars 74 forks source link

[Bug] useEffect IDisposable #342

Closed huudev closed 1 year ago

huudev commented 1 year ago

Dear developer team. I have a problem with useEffect that when i return IDisposable object it is not called every time count state changes. Please help me. Thanks.

let count = ctx.useState 0 ctx.useEffect ( handler = (fun _ -> printf "m1" { new IDisposable with member this.Dispose(): unit = raise (Exception "a") } ), triggers = [EffectTrigger.AfterChange count] )

JaggerJo commented 1 year ago

What exactly do you mean?

A: printf is not called B: the Dispose method is not called

huudev commented 1 year ago

Dispose

B. I try set breakpoint at Dispose method but it never hit.

JaggerJo commented 1 year ago

fixed. (wait for release 1.0.1, should be out in a few minutes)

huudev commented 1 year ago

fixed. (wait for release 1.0.1, should be out in a few minutes)

Thank you for your enthusiastic support. But i found useEffect's Dispose calling behavior is not the same as useEffect's cleanup calling behavior in Reactjs. In Reactjs the cleanup function call is performed when a new execution in useEffect but useEffect in FuncUI the Dispose function will be kept and executed when the component is destroyed. If this different behavior is your intention then no problem.

I would like to describe more useEffect behavior in reactjs (code reactjs)

  1. When start the program print "into useEffect" .
  2. When i click '+1' button the program print "call destroy" and print "into useEffect"

Thanks.

JaggerJo commented 1 year ago

@huudev this is intentionally so. Could you give a bit more context on why reacts execution strategy is handy/needed?

huudev commented 1 year ago

@huudev this is intentionally so. Could you give a bit more context on why reacts execution strategy is handy/needed?

One scenario that I often come across is calling http request in useEffect based on a state value and set the response of that request to other state. If Dispose is not called immediately when that value changes, then due to asynchronous reasons the response from previous requests (due execution useEffect from old values) ​​may be returned later and not get the desired result.