Fork me on GitHub
#reagent
<
2020-07-25
>
John Oerter14:07:37

Is this an idiomatic way to switch between different states of my app? I'm using cond with a keyword atom for each state.

[:main.inner.cover.flex-1 {:role "main"}                                                                                                          
     (let [state @app-state]                                                                                                                          
         (cond                                                                                                                                           
             (= :goal-input state) [goal-input the-goal app-state]                                                                                          
             (= :goal-timer state) [goal-timer the-goal]
             (= :goal-retro state) [:h1 "Goal Retro"]))]  

p-himik14:07:00

I would use case since all of the keywords are static.

p-himik14:07:32

Regarding the usage of the ratom itself - I think it's OK. But it's a very small case, it's hard to do something "bad" here.

John Oerter14:07:56

Ah perfect, thank you. If I use case then I wouldn't need the let anymore to deref the atom right?

p-himik14:07:40

Yep. And all those =.

John Oerter14:07:23

Nice. Thank you!

p-himik14:07:39

Ah, there's one difference between cond and case that may be important. With cond, if state is not equal to any of the keywords, the value will be nil. But with case, there will be an exception.

John Oerter14:07:23

Ah okay. But I can provide a default as well, right? Also, are keywords the best way to represent these states? Is there a better way?

p-himik14:07:34

Yeah, but the default needs to be explicit. Just something to keep in mind. Well, it depends. Seems fine in this case.

John Oerter18:07:10

Thanks! I appreciate the help

John Oerter18:07:41

Yeah coming from c#/typescript it feels weird, but I don't know of a better way at this point

David Pham05:07:32

You could also use multimethods or reitit for doing the routing.