Fork me on GitHub
#fulcro
<
2023-11-09
>
Eric Dvorsak10:11:46

in a form/defsc is it possible to setup a hook that uses the this? I tried that:

(hooks/use-effect (fn []
                              (app/register-push-handler :new-topic
                                                         (fn [message]
                                                           (log/info this)
                                                           (log/info (comp/get-ident this))
                                                           (log/info (comp/props this))

                                                           (form/input-changed! {::form/instance this}
                                                                                :topic/description "banana")))
                              (fn []
                                (app/deregister-push-handler :new-topic)))
                            [])
but comp/props is failing my intention is to have a chatbot stream inputs into the form via websocket

1
tony.kay16:11:10

Definitely do not do it this way. In my opinion you should never set up side effects in the UI if you can avoid it, and in this case you're trying to close over a thing that is mutable in react. In general, hooks are great for some things, but this is not one of them. What you want to do is register a function and pass it the ident of the UI component that you want to update. Then all you have to do is run mutations that update the data in app State at that ident, and the UI will follow along.

tony.kay16:11:12

I mean, you can use the hook to do the registering... But just grab the ident to begin with, so that you have a constant value to form the closure with

Eric Dvorsak16:11:38

Yeah I didn’t really want to use ‘this’ but that was what ‘input-changed’ needed

Eric Dvorsak17:11:52

I ended up using trigger directly as the params are not that hard to build up

tony.kay17:11:49

I’m actuall kind of surprised the props function didn’t work. I mean it is working on a mutable js object that React owns, but I would not have expected it to change.

tony.kay17:11:59

The dynamic vars will be out of context in the async callback, but props doesn’t require those.

tony.kay18:11:34

When Strict Mode is on, React will run one extra development-only setup+cleanup cycle before the first real setup. This is a stress-test that ensures that your cleanup logic "mirrors" your setup logic and that it stops or undoes whatever the setup is doing. If this causes a problem, implement the cleanup function.

tony.kay18:11:20

none of their examples show using this within the effect. Instead if there is something in the props needed by the effect it is first destructured and included in the dependencies.

tony.kay18:11:56

So I expect it is something you’re doing wrong or that the effect system of React is being weird about. but in general this should be usable in functions…how else would onClick work and allow the use of this

tony.kay18:11:30

Technically having “no deps” in the data dependency list seems a bit off, given that if you want something from props you clearly have a dependency?

Eric Dvorsak18:11:38

My understanding is that you make a useEffect with no dependencies when you want it to run on mount and unmount only

tony.kay19:11:53

correct, but then you should never use props within it

Eric Dvorsak19:11:26

I though I was capturing the scope

tony.kay19:11:44

React has gotten weirder and weirder internally as they’ve added hooks and async stuff. I would not bet on things like order of operations.

tony.kay19:11:30

that’s why they make you say what explicit data you depend on

tony.kay19:11:54

but I agree, you should have been creating a closure on this…seems reasonable to me

tony.kay19:11:37

I put that in the indexes as well and it always seems to work. So, I can’t really tell you why it wouldn’t, other than Fulcro isn’t “in charge” of these React details.

Eric Dvorsak19:11:41

my guess would be that on-mount "this" is not looking enough like an instance of form for comp/props?

Eric Dvorsak19:11:12

actually this works

(hooks/use-effect (fn []
                              (log/info :props (comp/props this))

                              (fn []
                                (app/deregister-push-handler :bitch)))
                            [])

Eric Dvorsak19:11:41

I guess what I was doing was just too illegal 😄

tony.kay19:11:48

I find it rather curious. Off the top of my head I can’t really say why it wouldn’t work other than timing.

tony.kay19:11:03

but I can’t imagine a timing that would mess it up 😄

Eric Dvorsak19:11:06

ok it's not the props, I made a simpler example (let [_ (hooks/use-effect (fn [] (reset! a (fn [] (form/input-changed! this :subject/description "hello"))) (fn [])) [])] (dom/div :.my-container (dom/button {:onClick #(@a)} (dom/label "DANGER")) getting the same issue but I can log the props the error happens after it was the wrong line

Eric Dvorsak19:11:41

@U0CKQ19AQ ok it had nothing to do with react: this works:

(let [_ (hooks/use-effect (fn []
                              (reset! a (fn []
                                          (js/alert (comp/get-ident this))
                                          (form/input-changed! {::form/form-instance this
                                                                ::form/master-form this} :subject/description "hello")))
                              (fn []))
                            [])]
    (dom/div :.my-container
             (dom/button {:onClick #(@a)} (dom/label "DANGER"))

Eric Dvorsak19:11:53

I was passing this it was expecting an env

Tobias Burger14:11:04

Fulcro RAD question: Has anyone used fo/field-visible? or fo/fields-visible? on blob attributes? I cannot make it work.

tony.kay16:11:17

It is entirely possible that I have not implemented all of the aspects on blob rendering. Check the sui plug-in code.

Tobias Burger14:11:51

Sorry, still learning... So basically the only thing missing is the rendering conditional that queries for the visibility when rendering the attribute field? As I plan to write my custom form rendering that should be easy to implement. Maybe I can be of use and provide a pull request for the sui plug-in but as I have a narrow timeline I cannot promise it.

tony.kay15:11:00

That is likely, correct. Would be in the SUI plug-in code. I usually trace to it using the all-controls map that you install at startup

👍 1