Skip to content
This repository was archived by the owner on Mar 28, 2025. It is now read-only.

Commit 0f662b7

Browse files
committed
defcc macro
1 parent 59c694b commit 0f662b7

File tree

5 files changed

+440
-385
lines changed

5 files changed

+440
-385
lines changed

README.md

+19-7
Original file line numberDiff line numberDiff line change
@@ -224,16 +224,16 @@ Each component in Rum has state associated with it. State is just a CLJS map wit
224224

225225
* `:rum/react-component` — link to React component/element object
226226
* `:rum/id` — unique component id
227-
* everything mixins are using for they internal bookkeeping
228-
* anything your own code put here
227+
* everything mixins are using for their internal bookkeeping
228+
* anything you’ve put there (feel free to populate state with your own stuff!)
229229

230-
Reference to current state is stored as `volatile!` boxed value at `props[":rum/state"]`.
230+
Reference to current state is stored as `volatile!` boxed value at `state[":rum/state"]`.
231231
Effectively state is mutable, but components do not change volatile reference directly,
232232
instead all lifecycle functions accept and return state value.
233233

234234
Classes define component behavior, including render function. Class is built from multiple mixins.
235235

236-
Mixins are basic building blocks for designing new components behaviors in Rum. Each mixin is just a map of one or more of following functions and maps:
236+
Mixins are basic building blocks for designing new components behaviors in Rum. Each mixin is just a map of one or more of following functions:
237237

238238
```clojure
239239
{ :init ;; state, props ⇒ state
@@ -246,9 +246,14 @@ Mixins are basic building blocks for designing new components behaviors in Rum.
246246
:wrap-render ;; render-fn ⇒ render-fn
247247
:did-update ;; state ⇒ state
248248
:will-unmount ;; state ⇒ state
249-
:get-child-context ;; ⇒ child-context
250-
:child-context-types ;; {context-types-for-children}
251-
:context-types ;; {context-types-for-component} }
249+
:child-context ;; state ⇒ child-context }
250+
```
251+
252+
Additionaly, mixin can specify following maps:
253+
254+
```clojure
255+
{ :child-context-types { ... }
256+
:context-types { ... } }
252257
```
253258

254259
Imagine a class built from N mixins. When lifecycle event happens in React (e.g. `componentDidMount`), all `:did-mount` functions from first mixin to last will be invoked one after another, threading current state value through them. State returned from last `:did-mount` mixin will be stored in volatile state reference by Rum. Similarly, `context` maps from multiple mixins are combined into one map.
@@ -323,6 +328,13 @@ This is a detailed breakdown of what happens inside of Rum. By using `rum/defc`,
323328

324329
## Changes
325330

331+
### 0.3.0
332+
333+
- [ BREAKING ] Component inner state (`:rum/state`) was moved from props to state. It doesn’t change a thing if you were using only Rum API, but might break something if you were relaying on internal details
334+
- Upgraded to React 0.13.3, Sablono 0.3.6, ClojueScript 1.7.48
335+
- Added `defcc` macro for when you only need React component, but not the whole Rum state
336+
- Deprecated `rum/with-props` macro, use `rum/with-key` or `rum/with-ref` fns instead
337+
326338
### 0.2.7
327339

328340
- Allow components to refer to themselves (thx [Kevin Lynagh](https://github.com/lynaghk), pull request #30)

examples/examples.cljs

+10-11
Original file line numberDiff line numberDiff line change
@@ -279,17 +279,16 @@
279279
:value (rum/react ref)
280280
:on-change #(reset! ref (.. % -target -value))}])
281281

282-
(rum/defcs restricting-input < rum/reactive [state ref fn]
283-
(let [comp (:rum/react-component state)]
284-
[:input {:type "text"
285-
:style {:width 170}
286-
:value (rum/react ref)
287-
:on-change #(let [new-val (.. % -target -value)]
288-
(if (fn new-val)
289-
(reset! ref new-val)
290-
;; request-render is mandatory because sablono :input
291-
;; keeps current value in input’s state and always applies changes to it
292-
(rum/request-render comp)))}]))
282+
(rum/defcc restricting-input < rum/reactive [comp ref fn]
283+
[:input {:type "text"
284+
:style {:width 170}
285+
:value (rum/react ref)
286+
:on-change #(let [new-val (.. % -target -value)]
287+
(if (fn new-val)
288+
(reset! ref new-val)
289+
;; request-render is mandatory because sablono :input
290+
;; keeps current value in input’s state and always applies changes to it
291+
(rum/request-render comp)))}])
293292

294293
(rum/defcs restricting-input-native < rum/reactive [state ref fn]
295294
(let [comp (:rum/react-component state)]

src/rum.clj

+12-1
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,19 @@
6262
[& body]
6363
(-defc 'rum/render-state->mixin body))
6464

65+
(defmacro defcc
66+
"Same as defc, but render will take additional first argument: react component
67+
68+
Usage:
69+
70+
(defcc name doc-string? [< mixins+]? [comp params*] render-body+)"
71+
[& body]
72+
(-defc 'rum/render-comp->mixin body))
73+
6574
(defmacro with-props
66-
"Calling function returned by defc will get you component. To specify
75+
"DEPRECATED. Use rum/with-key and rum/with-ref functions
76+
77+
Calling function returned by defc will get you component. To specify
6778
special React properties, create component using with-props:
6879
6980
(rum/with-props <ctor> <arg1> <arg2> :rum/key <key>)

src/rum.cljs

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[cljsjs.react]
55
[sablono.core]))
66

7-
(enable-console-print!)
7+
#_(enable-console-print!)
88

99
(let [last-id (volatile! 0)]
1010
(defn next-id []
@@ -160,6 +160,9 @@
160160
(defn render-state->mixin [render-fn]
161161
{ :render (fn [state] [(apply render-fn state (::args state)) state]) })
162162

163+
(defn render-comp->mixin [render-fn]
164+
{ :render (fn [state] [(apply render-fn (:rum/react-component state) (::args state)) state]) })
165+
163166
(defn args->state [args]
164167
{::args args})
165168

target/main.js

+395-365
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)