How to understand when I need to set the initial state of a component and whether to use lambda for it? Example:
(defsc HomePage [this props]
{:query [{[:component/id :session] (comp/get-query Session)}]
:initial-state (fn [p] {[:component/id :session] {}})
:ident (fn [] [:component/id :home-page])}
(dom/span "Hi " (get-in props [[:component/id :session] :account/username])))
(def ui-home-page (comp/factory HomePage))
(defsc Main [this {:keys [home-page] :as props}]
{:query [{:home-page (comp/get-query ui-home-page)}]
:initial-state (fn [p] {:home-page (comp/get-initial-state HomePage)})
:ident (fn [] [:component/id :main])
:route-segment ["main"]}
(dom/div (ui-home-page home-page)))
If I remove any of the two initial-state's, or change the second one to be a template {:home-page {}}
, it doesn't work: the username in the HomePage component is not loaded: why? I also don't understand why I need to assign initial value in HomePage to [:component/id :session], even though it is an empty initial value which is supposed to be replaced with the value from the app db. Why doesn't the query to the app db to get [:component/id :session] happen if there is no initial value?I have a report that lists products. Products can have many tags (ref attribute). What's the proper way to have the report render the tag labels instead of the idents that it lists by default?
See https://book.fulcrologic.com/#_a_warning_about_ident_and_link_queries Your Main init state is wrong, pass in the class and not ui-... Use the template form unless you need to do something it doesn't support. Simpler, better errors checking
Oops, sorry, I was trying out different things, same thing if I pass in the class.
Wait, I do pass in the class in the init state, and not the factory.
(I would have hoped that https://blog.jakubholy.net/2020/troubleshooting-fulcro/#_frontend_fulcro would have guided you to the 1st answer)
Sorry, it's query that's wrong
I have tried going with the troubleshooting guide!
Yeah, I was trying out random things, when it's a class same thing happens.
The section you linked to explains this, thank you!
I don't know why I missed the steps in the troubleshooting guide. Probably should have unfolded all (missed that link) and done Cmd-F for initial-state.
Please, go to the guide, click the Expand All link, search for " 6.8. A Warning About Ident and Link Queries" and tell what I can improve so that your younger I would have found it 🙏
I think I went with the data in DB missing/wrong because it mentioned initial state.
And the same thing happened with me again just now when I tried to find the section I knew I needed XD
But your data was actually correct, wasn't it? Was is XD?
XD is a laughing face.
Well it wasn't being populated in the component.
So there was no data.
Well, the data of Session was in the DB, it just did not show up in the HomePage UI,no?
Yes.
Any suggestions for how to make the distinction between the two clearer so that folk would go down the right path?
Make steps unfolded by default so that people could Cmd-F?
As I said, I missed the unfold all link.
The idea is that the questions should guide you to the answer though searching is certainly also a common use case. So I wanted to ask, how an I improve the question(s) so that you would not have gone down the wrong path?
Well I guess when I am searching for a solution to a problem I try to skim as much as possible, and having to unfold all the points manually prevents me from that. I missed the explanation in 2. saying to go to the next section, plus when 2. is unfolded there is the initial state section, which seemed like the right section for me so I hadn't thought to look in the others. I would suggest adding the link to this initial state problem into the section 2. too, even though it doesn't strictly fall under it.
Maybe an additional section for people like me for reverse search? Like "if you are having problems with initial state, these are the possible reasons". Btw in this case, I initially didn't know what's happening at all, just randomly tried adding the initial state to the components at some point.
But you did not have a problem with initial state. Your only problem was that the data in the DB were not showing up in the expected UI. At leas that is how I see it. The idea with having everything collapsed is so that you see all the questions at the same level at once and can better decide which one matches best. (the mention to look at the next point I've added after our initial discussion)
I mean, sure, if you had magically told me that while I was on the page, I would have looked in the correct section.
But we are talking about people trying to find an answer to their question. People are skimming, making mistakes, getting distracted, getting side-tracked, misunderstanding what's written.
My philosophy is that everything should be written with that in mind.
When you are traversing the tree, any mistake at any point will lead you astray.
These are good points. I will tru to add some cross-references. And I will think about how to help people ask the right questions / discard the wrong ones. Thank you very much for your inputs!!!