Skip to content

Added onDispose Functionality and Modified deps Type to Array in useEffect - #13

Closed
psyrenpark wants to merge 1 commit into
pavi2410:mainfrom
psyrenpark:patch-3
Closed

Added onDispose Functionality and Modified deps Type to Array in useEffect#13
psyrenpark wants to merge 1 commit into
pavi2410:mainfrom
psyrenpark:patch-3

Conversation

@psyrenpark

Copy link
Copy Markdown

Hello ~,

I needed an onDispose functionality within useEffect, so I've added it. Additionally, I have changed the type of deps to an array.

I would appreciate it if you could review them.

// example

    /*
    useEffect(()=>{
        console.log("Call once")
    },[])
    */
    useEffect(onEffect = {
        println("Call once")
    })

    /*
    useEffect(()=>{
        console.log("Call once")
        return  () => {
            console.log("onDispose")
        }
    },[])
    */
    useEffect(onEffect = {
            println("Call once")
        }, onDispose = {
            println("onDispose")
        }
    )

    /*
    useEffect(()=>{
        console.log("Call when the value of isOn changes")
        return  () => {
            console.log("onDispose")
        }
    },[isOn, "SOME" ])
    */
    useEffect(arrayOf(isOn,"SOME"),
        onEffect =  {
            println("Call when the value of isOn changes  [isOn] $isOn")
        }, onDispose = {
            println("onDispose")
        }
    )

Hello,

I have made some modifications while using it.

I would appreciate it if you could review them.
@junerver

junerver commented Mar 8, 2024

Copy link
Copy Markdown

In fact, every timeif you pass deps through arrayOf, a new array instance will be created, which will cause LaunchedEffect and DisposeEffect to execute blocks. If you just want to execute when the component is loaded or when it ends, you can declare a custom hook. For example: useMount, useUnmount;

Of course, if you insist on doing this through a hook, then your deps should be vararg deps:Any, and use *deps when passing it to XXEffect so that it can be tracked correctly.

@pavi2410

Copy link
Copy Markdown
Owner

I'm not accepting this PR in favor of ComposeHooks and in v2 of this library, this module is removed (See #16).

@pavi2410 pavi2410 closed this Sep 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants