Search code examples
clojurescriptom

Differences between :init-state vs :state at build function


I understand that you can initiate the state of a component by passing a map as the value of the :init-state keyword. But by passing this map as the value of the :state keyword, for example, between a component and its child component, now they can share that same state? Is that it? Thanks.


Solution

  • The difference is when the childs state gets set.

    • :init-state is only set once, when the component is mounted.
    • :state is set on each render.

    Therefore, :init-state should be used to (as the name suggests), initialise the state. On the other hand, :state is used to set state that changes over time.