Fork me on GitHub
#re-frame
<
2022-09-20
>
popeye17:09:55

I have a radio button defined and one of teh item value is selected by default , My functionality is working as expected for on-change, But how can i call dispatch function for default checked item ?

{:id value
       :key key
       :name-const id
       :type "radio"
       :default-checked selected
       :value value 
       :on-change #(dispatch :user-selects-item id (-> % .-target .-value))} 

p-himik17:09:43

So you want to call dispatch when the radio button is rendered for the first time?

p-himik17:09:27

Where does the selected come from in the first place?

popeye17:09:31

selected is a boolean value, for which item is selected

p-himik17:09:24

I understand what it means. But where does it come from?

p-himik17:09:29

What is its origin?

popeye17:09:34

initially , by default few values are inserted it to db, and in the function, when item matches in the list, by default radio button will be checked for that item

popeye18:09:46

something I want like on-load of radio button

p-himik18:09:52

From re-frame's perspective, there should not be any dispatch here at all when you just display that radio button for the first time. You already have all the necessary data in the app-db, and absolutely everything else in your app should be a consequence of a direct user action (or some background activity, like js/setTimeout and whatnot) given a particular app-db state. Clicking a button is a direct action, loading the app is a direct action. But something being rendered because or some action is not a direct action by itself.

p-himik18:09:33

Something has led to that radio button being shown in its particular state. That very "something" is responsible for all the initial processes.

popeye18:09:06

i wrote a function which calls the dispatch on function load

popeye18:09:20

but that is calling each time when value changes as well

popeye18:09:19

oh I did one more thing now!!! I surrounded my radio component in a fn like below

`  (defn hello []
    (dispatch-intial load)
    (fn []
      {:id              value
       :key             key
       :name-const      id
       :type            "radio"
       :default-checked selected
       :value           value
       :on-change       #(dispatch :user-selects-item id (-> % .-target .-value))}))

popeye18:09:38

and called a function (dispatch-intial load) first time

popeye18:09:45

is that valid ?

p-himik18:09:57

It is valid and is likely to work unless you use :key on that hello. But it's not in accordance to what the re-frame docs preach.

popeye03:09:20

I agree, I thank you for your response

popeye03:09:33

I will update myself with mount