Skip to content

init-state does not work in mixins #28

@danielytics

Description

@danielytics

I'm not sure if this is related to #25 or not and its very possible that I'm simply using this wrong (or that its not supported, although the docstrings say that it is). The following test code prints nil instead of "test" as I expect:

(defmixin test-mixin
  (init-state [_]
    (println "init-state")
    {:foo "test"}))

(defcomponentk test-component []
  (:mixins test-mixin)
  (render-state [_ {:keys [foo]}]
    (println foo)
    (dom/div foo)))

Note that "init-state" is printed, so it does get called - the state simply doesn't get merged into the child.


A failed workaround that I tried is to set the state in will-mount instead, but this doesn't work because if the component tries to access the state in its own will-mount, this state won't have been set (mixin lifecycle functions seem to get called after the components lifecycle functions).

The workaround I eventually went with is less than ideal:

(defmixin test-mixin
  (init [_ & [s]]
    (let [my-s {:foo "test"}]
      (if s
        (merge my-s s)
        my-s))))

(defcomponentk test-component []
  (:mixins test-mixin)
  (init-state [_]
    (.init owner {:bar "test2"}))
  (render-state [_ {:keys [foo]}]
    (println foo)
    (dom/div foo)))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions