Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refined useEffectOnce to solely act as an alias for React.useEffect(effect, [||]). #600

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 3 additions & 43 deletions Feliz/Internal.fs
Original file line number Diff line number Diff line change
Expand Up @@ -63,55 +63,15 @@ module Internal =

[<Hook>]
let useEffectOnce(effect: unit -> unit) =
let calledOnce = Interop.reactApi.useRefInternal false

useEffectWithDeps (fun () ->
if not calledOnce.current then
calledOnce.current <- true
effect()
) [||]
useEffectWithDeps effect [||]

[<Hook>]
let useEffectDisposableOnce (effect: unit -> #IDisposable) =
let destroyFunc = Interop.reactApi.useRefInternal None
let calledOnce = Interop.reactApi.useRefInternal false
let renderAfterCalled = Interop.reactApi.useRefInternal false

if calledOnce.current then
renderAfterCalled.current <- true

useEffectDisposableOptWithDeps (fun () ->
if calledOnce.current
then None
else
calledOnce.current <- true
destroyFunc.current <- effect() |> Some

if renderAfterCalled.current
then destroyFunc.current
else None
) [||]
useEffectDisposableOptWithDeps (effect >> Some) [||]

[<Hook>]
let useEffectDisposableOptOnce (effect: unit -> #IDisposable option) =
let destroyFunc = Interop.reactApi.useRefInternal None
let calledOnce = Interop.reactApi.useRefInternal false
let renderAfterCalled = Interop.reactApi.useRefInternal false

if calledOnce.current then
renderAfterCalled.current <- true

useEffectDisposableOptWithDeps (fun () ->
if calledOnce.current
then None
else
calledOnce.current <- true
destroyFunc.current <- effect()

if renderAfterCalled.current
then destroyFunc.current
else None
) [||]
useEffectDisposableOptWithDeps effect [||]


let createContext<'a> (name: string option) (defaultValue: 'a option) =
Expand Down