Fork me on GitHub

can anyone speak to a reframe strategy for dynamically created components? whats a good way to attach a dynamic piece of state to the app-db, and to clean it up after it’s not needed? do you have a parent component handle some :create-thing where you generate a random id? do you redispatch another event targetted towards a component and let it handle creating itself?


@lwhorton: you appear to have a very component-oriented mindset. It's a bit like components are the "instigators" of things - its like they are the main "agents" in the architecture.


But, in the re-frame/Reagent approach, components are simply reflecting/rendering the current state in app-db.


They are not "actively" doing anything. They are not causing changes, and they are not "driving the app forwards" in any way.


Instead a re-frame app "moves forwards" (changes) via events


The handlers of events mutate app-db and that's how an app progresses.


So, to finally answer your question, these "dynamically created components" of which you speak will only "appear" because of some state which appears in app-db. And that state in app-db was put in place by some event handler. And that event handler was run because of some event. So it is "events" that are the genesis of all change. So instead of thing about "components" being somehow active and causing things to happen, think instead of events happening and app-db changing, and the components simply rendering that change.


So something like "cleaning up" will happen because an event happens, and an event-handler changes app-db (cleans up some data in it) in response.


@lwhorton i have felt this pain too, here is my approach


Thanks @mikethompson for the clarification. I still wonder, though - are we giving up modularity by having “someone else” manage our component data? My line of thinking is that as a component, *I* am the one responsible for the shape of my state, because I’m the only one who needs to understand it. The shape of the data I consume can come from a db somewhere and be formatted to fit my expected shape. However, I wouldn’t want to couple myself to my parent by requiring that a parent’s handler understand my innards. Does that make sense?


To clarify, if I had a dynamic list of todos, and a button that says ‘create todo list’. :create-todo-list happens -> db gets updated in handler: {:todo-lists (conj (:todo-lists db) (create-todo-list))} -> parent/root runs a (for [[id list-state] @todos] …).


That create-todo-list has absolutely nothing to do with the parent that handled the :create-todo-list — it doesn’t need to know what the shape of a todo-list is, and it definitely doesn’t want to have to change each and every time the todo-list needs to change state shape.


My current solution is to have (ns todo-list.core) expose its own (create-todo-list [id] {:shape “of” :the “data”})


Do you see what I mean about modularity?


@lwhorton: I think I see what you mean, but re-frame philosophy is the other way around: the components don't need to know what's the shape of the data in the app db, and the subscriptions are responsible for reshaping data for consumption in the components


Having said that, nothing stops you from defining the shape in the component, let's say "at creation time" and send that shape of data in the event, have the handlers to blindly take that data and put it into the db, etc... but then you have to be very careful if you are going to use the data in some other place different than your component


So to put this in real terms… I would have a parent - lets call it PA, at a “top level”, responsible for maintaining a list of todo-lists.


A button is pressed - and that button is rendered as part of PA, which dispatches :create-todo-list.


A PA registered handler captures the event, then places somewhere in the db the shape of the todo list attached to a uuid.


A PA registered subscriber detects the change to todos, then renders in a (for …) each todo-list, populating the individual lists’ state.


what do you mean by "A PA registered handler"? Is your PA component responsible of registering the re-frame handler?


A handler registered in re-frame by the PA component*


so when is that handler registered? on :component-will-mount?


Let’s just say that PA is always around. As part of the bootstrap process PA calls register-handler


Ok, but it's confusing to me. The term component in the context of a re-frame app usually refers to a reagent component that takes data from the app db via re-frame subscriptions, etc... it doesn't register any handler.


(it can be any React component of course, but it's usually created through reagent)


That brings up a good point, though. I never really thought about the need (or if its possible) to do subscription/handler registering and deregistering.


The react lifecycle events seem like the obvious place, but I would think that if a view isn’t around from the beginning, some parent is responsible for the handlers and subscriptions.


Cool. I use to do that as part of some bootstrap process, the same as you are doing, the only issue maybe is that you are mixing a component there that I'm not sure what it is simple_smile


To go back to the original point:


How then, does one say “mark all completed” for a particular todo-list? Is the event somehow tagged with the lists’ id? At create-time is the list handed down a function to invoke (a plain callback) by PA which does the dispatching with the ID tagging?


I use to think of it separate from the react components, and it usually runs before mounting any component


My first thought (and this is probably wrong) is that a todo-list (when rendered the first time) would be handed an id to use to dynamically subscribe to something based on id.


yes, I'd say the event is probably tagged with the list id


(defn todo-list [id] (let [state (reframe/subscribe [:todos id] …)


a todo-list can be handed an id so it subscribes to fetch the rest of the data, or it can be handed the todo-list data as props, so it doesn't need to get more data. that depends on how your app is structured


yes, that looks good to me


So for a :mark-all-completed id event, that handler would be part of PA, not registered by todo-list


If PA is your bootstrap process, so yeah it's probably registered there. Definitely not registered by todo-list


I guess at the highest level I’m really just trying to figure out where its appropriate to do registering of handlers and subscriptions in a way that is scalable.


By scalable I mean you don’t have one giant parent that handles every single event and subscription related to everything in your app.


I'd try to not think about it as a "parent"


From the outside looking in I would like to think of “component” as a complete set of handlers, subscriptions, and view(s)… because that’s how I’ve always built systems to be modular and scalable.


i.e. all that I need to do to have “this certain functionality” is to bring in that complete component… and hook up maybe one or two wires, but all-in-all its pretty self-sustaining.


In some scenarios that makes sense, it's just that the view is already composed of component so it's confusing, at least to me. And also, in some apps that's not even enough: imagine if you have different views from different "higher level components", consuming the same data... so having the views mixed with the handlers and subs would be not enough


I see. This has been very helpful. So if I might ask, how do you structure an app to avoid a big-blob-of-registrations in 1 or 2 files, and really thin reagent views everywhere else? How do you maintain a list of “available subscriptions” that a view can utilize?


I have to run. I was writing a long comment about having todo.views todo.handlers todo.subs namespaces... but you could also do it the other way around: views.todo handlers.todo subs.todo... depending on the scenario I'd use one or another...


Well either way, much appreciated. Thanks for your help.


No problem, good luck with that simple_smile


for those curious, after rethinking the above, I maintain modularity but also shift towards the re-frame “way" by having a /src/todo_list/core.cljs that contains all the register-sub, register-handlers, and initializing state fns necessary to operate a todo-list. a consumer of todo-list can simply dispatch [:todo-list/create-instance], which is handled inside of /todo_list/core.cljs itself where it assigns a uuid and places it into the database. the consumer can subscribe to :todo-list/instances, then render each todo via (for [[id props] @todos] ^{:key id} [todo-list id props]). By handing down this id at render-time, the todo-list knows “who it is” and can appropriately dispatch [:todo-list/some-event id args].


Consumers don’t need to know any internals about a todo-list, they simply dispatch an event that creates one. The burden is placed on the todo-list implementation to maintain separate instances and tie events to ids. If anyone sees some downsides to this approach, feel free to let me know how dumb I am.


Has anyone had experience using RethinkDB with Re-Frame?