Skip to content

Commit 6caaeb5

Browse files
committed
Doc updates
1 parent abc0bfa commit 6caaeb5

File tree

2 files changed

+46
-26
lines changed

2 files changed

+46
-26
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22

33
- Hot reloading now replaces methods in a React component's prototype instead of saving and
44
restoring. This is safer and doesn't cause `component-did-mount` to fire.
5-
- A component now implements the IFn protocol, allowing method calls, e.g.,
5+
- Components now implement the `IFn` protocol, allowing method calls, e.g.,
66
`(react/call instance :do-stuff)`, to be shortened to, e.g., `(instance :do-stuff)`.
7+
- State and props are duplicated as plain JavaScript objects to make them easy to view in React's
8+
developer tools for Chrome.
9+
- Including `:trace? true` when creating a class will cause the component to log every method call.
10+
- `create-element` will now accept a plain JavaScript React element.
11+
- Safer internal variable handling to make sure things don't break with advanced optimizations.

README.md

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,20 @@ A ClojureScript wrapper for React.
44

55
There are a number of React-like libraries for ClojureScript (e.g., Om, Reagent, Rum). Many use React under the covers.
66

7-
By contrast, this library simply exposes an interface to React in a ClojureScript-friendly way. It is for developers who, like myself, want to use React directly from a ClojureScript application.
7+
By contrast, this library simply exposes an interface to React in a ClojureScript-friendly way. It is for developers who, like myself, want to use React directly from ClojureScript.
8+
9+
## Goodies
10+
11+
- **Built-in support for hot-reloading**. If you use, for example, Figwheel to hot-reload files on change, React components created with the `defc` macro will be patched automatically.
12+
- **Method tracing**. Including `:trace? true` in the class definition map will cause every method call to emit a message to the console. This also attempts to break infinite loops by setting a ceiling on the number of traces in a short time period.
13+
- **React Developer Tools support**. Copies component state and props into plain JavaScript in non-optimized compilation modes so it is easier to use React Developer Tools (Chrome extension) to inspect components.
814

915
### Add dependency:
1016

1117
```clj
1218
[dmohs/react "0.2.12"]
1319
```
1420

15-
### Quickstart Template:
16-
17-
```bash
18-
lein new dmohs.cljs-react my-project
19-
```
20-
21-
See [dmohs.cljs-react template](https://github.com/dmohs/cljs-react-template) for usage.
22-
2321
## Top-Level API
2422

2523
The Top-Level API closely follows React's Top-Level API:
@@ -49,7 +47,7 @@ https://facebook.github.io/react/docs/top-level-api.html
4947
(.focus (@refs "clickable-div")))}))
5048
```
5149

52-
or, using the `defc` macro:
50+
or, using the `defc` macro (preferred and supports hot-reloading):
5351

5452
```clj
5553
(react/defc MyComponent
@@ -58,20 +56,31 @@ or, using the `defc` macro:
5856
...})
5957
```
6058

61-
The `render` method can return either an element or a vector (as in the above example).
59+
The `render` method can return either an element or a vector (as in the above example). Pass `:trace? true` for method tracing:
60+
61+
```clj
62+
(react/defc MyComponent
63+
{:trace? true
64+
:get-initial-state ...
65+
:render ...
66+
...})
67+
```
6268

6369
### React.createElement
6470

6571
```clj
66-
(def component (react/create-element MyComponent {:initial-click-count 15}))
72+
(react/defc MyComponent ...)
73+
(def PlainReactComponent (js/React.createClass ...))
6774
(react/create-element
6875
:div {:className "alert" :style {:backgroundColor "red"}}
69-
component)
76+
(react/create-element MyComponent {:foo "foo"})
77+
(react/create-element PlainReactComponent {:bar "bar"}))
7078

7179
;; Vector syntax
7280
(react/create-element [:div {:className "alert"}
7381
"Child 1" "Child 2"
74-
[MyComponent {:initial-click-count 15}]])
82+
[MyComponent {:initial-click-count 15}]
83+
[PlainReactComponent {:initial-click-count 21}]])
7584
```
7685

7786
### React.cloneElement
@@ -93,11 +102,9 @@ The `render` method can return either an element or a vector (as in the above ex
93102
### ReactDOM.render
94103

95104
```clj
96-
(react/render element container callback hot-reload?)
105+
(react/render element container callback)
97106
```
98107

99-
If `hot-reload?` is true, component state will be preserved before unmounting and loaded into the newly-rendered tree. This makes Figwheel usage quite easy. I usually pass `goog.DEBUG` as this parameter so it is automatically turned off for minimized/production builds.
100-
101108
### ReactDOM.unmountComponentAtNode
102109

103110
```clj
@@ -107,7 +114,7 @@ If `hot-reload?` is true, component state will be preserved before unmounting an
107114
### ReactDOM.findDOMNode
108115

109116
```clj
110-
(create/find-dom-node element)
117+
(react/find-dom-node element)
111118
```
112119

113120
## Component Specifications
@@ -116,16 +123,18 @@ Component specifications closely follow React's Component Specifications:
116123

117124
https://facebook.github.io/react/docs/component-specs.html
118125

119-
React methods are defined using Clojure naming conventions (`:get-initial-state` corresponds to `getInitialState`). Additional methods become part of the object, so `:add-foo` can be called like so:
126+
React methods are defined using Clojure naming conventions (`:get-initial-state` corresponds to `getInitialState`). Additional methods become part of the object (as in React), so `:add-foo` can be called like so:
120127
```clj
121128
(react/call :add-foo this "argument 1" "argument 2")
122129
;; or
123130
(react/call :add-foo (@refs "some-child") "argument 1" "argument 2")
124131
```
125132

126-
The special case of calling `this` can be shortened to:
133+
Additionally, components implement `IFn`, so the above calls can be shortened to:
127134
```clj
128-
(react/call-self :add-foo "argument 1" ...)
135+
(this :add-foo "argument 1" "argument 2")
136+
;; and
137+
((@refs "some-child") :add-foo "argument 1" "argument 2")
129138
```
130139

131140
Methods are passed a map with the appropriate keys defined:
@@ -144,12 +153,18 @@ Methods are passed a map with the appropriate keys defined:
144153
}
145154
```
146155

147-
1. This is used when you would pass a callback to `setState`, e.g., `(after-update #(.focus (react/find-dom-node this)))`.
148-
2. The `refs` atom allows accessing `this.refs` as a map (e.g., `(.focus (@refs "my-text-box"))`).
149-
3. Convenience atom for local variables. Instead of, e.g., `(set! (.-myTimer this) (js/setTimeout ...))`, you can do `(swap! locals assoc :my-timer (js/setTimeout ...))`.
156+
1. This is used when you would pass a callback to `setState`, e.g.,
157+
158+
`(after-update #(.focus (react/find-dom-node this)))`.
159+
2. The `refs` atom allows accessing `this.refs` as a map, e.g., `(.focus (@refs "my-text-box"))`.
160+
3. Convenience atom for local variables. Instead of, e.g.,
161+
162+
`(set! (.-myTimer this) (js/setTimeout ...))`, you can do
163+
164+
`(swap! locals assoc :my-timer (js/setTimeout ...))`.
150165

151166
Note: for non-api methods (like `:add-foo` above), this map is the first argument before any arguments passed when calling the method using `react/call`.
152167

153168
Modifying the `state` atom implicitly calls `this.setState`. This maintains the behavior of React's `this.state` in the way that updates (via `swap!` or `reset!`) are not visible in `@state` until after the component is re-rendered.
154169

155-
Note that `propTypes`, `statics`, and `mixins` are not yet implemented.
170+
Note that `propTypes`, `statics`, and `mixins` are not yet implemented. They may never be, since Clojure to some extent obviates the need for some of these utilities.

0 commit comments

Comments
 (0)