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.

Proposal to add something like reacts 'useEffect

See original GitHub issue

I thought about adding something like Reacts useEffect to simplify interop with some existing Avalonia Controls (CefGlue for Example)

Browser.create [
    Browser.startUrl "duckduckgo.com"
    // not sure how to trigger
    Browser.useEffect (fun browser -> browser.GoBack())
]

Implementing this feature is not complicated, maybe adding more ‘hook’ for attach und detach could also be helpful for controls that need to be specifically initialised.

Maybe effects should also be located in a new namespace like Avalonia.FuncUI.DSL.Effects, because they introduce side effects to an otherwise pure DSL.

@uxsoft @AngelMunoz @AngelMunoz opinions ?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
JaggerJocommented, Mar 21, 2020

@Swoorup useEffect allows you to do something with the actual instance of the control. I think it should be executed every time (at least by default).

1reaction
AngelMunozcommented, Feb 2, 2020

Not exactly sure why useEffect is required to access the document in JavaScript at all.

It’s just showcasing that you can do a mutation (hence the useEffect name) on an external element/control of the component it could have been a console.log too, it just shows that technically your component can be written in a pure functional way but allowing it to do side effects

This would translate to this in FuncUI, accessing some global state is easy.

I thought global on a more “global” scale like if you could access things that are at namespace level “like” window it would mean it would have been defined earlier in the module. perhaps I’m just not looking it on the right direction

MediaPlayer.create [
    MediaPlayer.filePath "song.mp3"

    // so something impure with the control
    MediaPlayer.useEffect (fun (player: MediaPlayer) -> 
        if player.state != state.playstate then
            match state.playstate with
            | Play -> player.Play()
            | Pause -> player.Pause()
    )
]

Yeah I think this gives me a better insight on how could they be used, you still need to use your current state to allow it to do what you need.

That could address stateful components coming from other C# libraries indeed. Are there any concerns about F# authored controls? I guess in the end even if you choose F# you still need to to the usual UserControl inheritance and property declaration thing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A complete Guide to React's proposed new use() hook
React Core team has come up with a new API proposal called the use() hook. This new hook will ensure first-class promise support...
Read more >
A complete guide to the useEffect React Hook
A comprehensive guide to the useEffect React Hook, including when and when not to use it, using it with custom Hooks, and much...
Read more >
'React Hook useEffect has a missing dependency' warning ...
One answer to a similar question was to put the function outside the component, but that would require me to pass a lot...
Read more >
Replacing Component Lifecycles with the useEffect Hook, ...
A walkthrough to help you convert React component lifecycles into Hooks using the useEffect Hook.
Read more >
Making Sense of React Hooks
If the React community embraces the Hooks proposal, it will reduce the number of concepts you need to juggle when writing React applications....
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