Search code examples
reagentfigwheelre-frame

Re-frame, reagent component life cycle and figwheel


I'm trying to learn a lot of things at the same time, so my question could be a little off target.

I have created a re-frame-template project with lein and I have my default main component:

(defn main-panel []
  (let [name (subscribe [:name])]
    (fn [] [:div [:h1 @name]])))

I wanted to add an event listener for keypress, but I couldn't manage to do it correctly: if I only set (using domina) the (listen! ...) in the :component-did-mount, on every figwheel reload a new even listener is added to the old one(s). If I set the unlisten! in component-will-unmount, I get no result on keypress.

What I noticed is that on every figwheel reload, the :component-will-unmount is triggered immediatly after the :component-did-mount!

What am I doing wrong?

Thanks!


Solution

  • Ok, I've found my mistake. I had to call the react "methods" with with-meta.

    So, for example:

    (with-meta myComponent {:component-did-mount #(ev/listen! ...)
                            :component-will-unmount #(ev/unlisten! ...)})