2014-02-15 16:29:37 +00:00
|
|
|
|
(ns reagentdemo.news.anyargs
|
2015-07-31 07:58:07 +00:00
|
|
|
|
(:require [reagent.core :as r]
|
2015-02-09 20:02:31 +00:00
|
|
|
|
[reagentdemo.syntax :as s]
|
2015-09-08 18:18:27 +00:00
|
|
|
|
[sitetools.core :as tools :refer [link]]
|
2014-02-15 16:29:37 +00:00
|
|
|
|
[reagentdemo.common :as common :refer [demo-component]]
|
|
|
|
|
[geometry.core :as geometry]))
|
|
|
|
|
|
2015-09-08 18:18:27 +00:00
|
|
|
|
(def url "/news/any-arguments.html")
|
2014-11-29 18:51:45 +00:00
|
|
|
|
(def title "All arguments allowed")
|
2014-11-29 17:30:24 +00:00
|
|
|
|
|
2014-12-07 18:26:20 +00:00
|
|
|
|
(def ns-src (s/syntaxed "(ns example
|
2015-07-31 07:58:07 +00:00
|
|
|
|
(:require [reagent.core :as r]))"))
|
2014-02-15 16:29:37 +00:00
|
|
|
|
|
|
|
|
|
(defn hello-component [name]
|
|
|
|
|
[:p "Hello, " name "!"])
|
|
|
|
|
|
|
|
|
|
(defn say-hello []
|
|
|
|
|
[hello-component "world"])
|
|
|
|
|
|
|
|
|
|
(defn geometry-example []
|
|
|
|
|
[geometry/main {:width "100%" :height 500}])
|
|
|
|
|
|
|
|
|
|
(defn my-div []
|
|
|
|
|
(let [this (r/current-component)]
|
|
|
|
|
(into [:div.custom (r/props this)]
|
|
|
|
|
(r/children this))))
|
|
|
|
|
|
|
|
|
|
(defn call-my-div []
|
|
|
|
|
[:div
|
|
|
|
|
[my-div "Some text."]
|
|
|
|
|
[my-div {:style {:font-weight 'bold}}
|
|
|
|
|
[:p "Some other text in bold."]]])
|
|
|
|
|
|
|
|
|
|
(defn main [{:keys [summary]}]
|
2014-11-29 18:51:45 +00:00
|
|
|
|
(let [geometry {:href "https://github.com/reagent-project/reagent/tree/master/examples/geometry"}
|
2014-02-15 16:29:37 +00:00
|
|
|
|
jonase {:href "https://github.com/jonase"}]
|
|
|
|
|
[:div.reagent-demo
|
2015-09-08 18:18:27 +00:00
|
|
|
|
[:h1 [link {:href url} title]]
|
2017-11-28 17:16:26 +00:00
|
|
|
|
[:span "2014-02-15"]
|
|
|
|
|
|
2014-02-15 16:29:37 +00:00
|
|
|
|
[:div.demo-text
|
2014-02-16 07:40:52 +00:00
|
|
|
|
|
|
|
|
|
[:h2 "If it looks like a function…"]
|
|
|
|
|
|
|
|
|
|
[:p "Calling a component in Reagent looks a lot like a function
|
|
|
|
|
call. Now it also " [:em "works"] " like one."]
|
2014-04-01 17:50:28 +00:00
|
|
|
|
|
2014-02-16 07:40:52 +00:00
|
|
|
|
[:p "Before 0.4.0, component functions were always called with
|
|
|
|
|
three arguments: a map of attributes, a vector of ”children”,
|
|
|
|
|
and the current React component."]
|
2014-02-15 16:29:37 +00:00
|
|
|
|
|
2014-02-16 07:40:52 +00:00
|
|
|
|
[:p "This was confusing, and an unnecessary limitation, so now
|
|
|
|
|
component functions get exactly the same arguments you pass to
|
|
|
|
|
them."]
|
2014-02-15 16:29:37 +00:00
|
|
|
|
|
|
|
|
|
(if summary
|
2015-09-08 18:18:27 +00:00
|
|
|
|
[link {:href url :class 'news-read-more} "Read more"]
|
2014-02-15 16:29:37 +00:00
|
|
|
|
[:div.demo-text
|
|
|
|
|
[:p "In other words, you can now do this:"]
|
|
|
|
|
|
2014-11-29 17:30:24 +00:00
|
|
|
|
[demo-component {:comp say-hello
|
2014-12-07 18:26:20 +00:00
|
|
|
|
:src (s/src-of [:hello-component :say-hello])}]
|
2014-02-15 16:29:37 +00:00
|
|
|
|
|
|
|
|
|
[:p "In the above example, it wouldn’t make any difference at
|
2014-02-16 07:40:52 +00:00
|
|
|
|
all if " [:code "hello-component"] " had been called as a
|
|
|
|
|
function, i.e with parentheses instead of brackets (except
|
|
|
|
|
for performance, since components are cached between renders
|
|
|
|
|
if the arguments to them don’t change)."]
|
2014-02-15 16:29:37 +00:00
|
|
|
|
|
|
|
|
|
[:p "But there is one drawback: component function no longer
|
2014-02-16 07:40:52 +00:00
|
|
|
|
receives the ”current component” as a parameter. Instead
|
|
|
|
|
you’ll have to use "
|
|
|
|
|
[:code "reagent.core/current-component"]
|
|
|
|
|
" in order to get that. Beware that "
|
|
|
|
|
[:code "current-component"] " is only valid in component
|
|
|
|
|
functions, and must be called outside of e.g event handlers
|
|
|
|
|
and " [:code "for"] " expressions, so it’s safest to always
|
|
|
|
|
put the call at the top, as in " [:code "my-div"] " here:"]
|
2014-02-15 16:29:37 +00:00
|
|
|
|
|
2014-11-29 17:30:24 +00:00
|
|
|
|
[demo-component {:comp call-my-div
|
2014-12-07 18:26:20 +00:00
|
|
|
|
:src [:pre
|
|
|
|
|
ns-src
|
|
|
|
|
(s/src-of [:my-div :call-my-div])]}]
|
2014-02-15 16:29:37 +00:00
|
|
|
|
|
2014-02-16 07:40:52 +00:00
|
|
|
|
[:p [:em "Note: "] [:code "r/props"] " and "
|
2014-12-07 18:26:20 +00:00
|
|
|
|
[:code "r/children"] " correspond to React’s "
|
|
|
|
|
[:code "this.props"] " and " [:code "this.props.children"] ",
|
2014-02-16 07:40:52 +00:00
|
|
|
|
respectively. They may be convenient to use when wrapping
|
|
|
|
|
native React components, since they follow the same
|
|
|
|
|
conventions when interpreting the arguments given."]
|
|
|
|
|
|
|
|
|
|
[:h2 "Other news in 0.4.0"]
|
|
|
|
|
|
|
|
|
|
[:ul
|
|
|
|
|
|
2014-02-21 15:24:04 +00:00
|
|
|
|
[:li "React has been updated to version 0.9.0."]
|
|
|
|
|
|
2014-02-16 07:40:52 +00:00
|
|
|
|
[:li "You can now use any object that satisfies "
|
|
|
|
|
[:code "ifn?"] " as a component function, and not just
|
|
|
|
|
plain functions. That includes functions defined with "
|
|
|
|
|
[:code "deftype"] ", " [:code "defrecord"] ", etc, as well
|
|
|
|
|
as collections like maps."]
|
|
|
|
|
|
|
|
|
|
[:li
|
|
|
|
|
[:code "reagent.core/set-state"] " and "
|
|
|
|
|
[:code "reagent.core/replace-state"] " are now implemented
|
|
|
|
|
using an " [:code "reagent.core/atom"] ", and are
|
|
|
|
|
consequently async."]
|
|
|
|
|
|
|
|
|
|
[:li "Keys associated with items in a seq (e.g ”dynamic
|
|
|
|
|
children” in React parlance) can now be specified with
|
|
|
|
|
meta-data, as well as with a " [:code ":key"] " item in the
|
|
|
|
|
first parameter as before. In other words, these two forms
|
|
|
|
|
are now equivalent: " [:code "^{:key foo} [:li bar]"] "
|
|
|
|
|
and " [:code "[:li {:key foo} bar]"] "."]
|
|
|
|
|
|
|
|
|
|
[:li "Performance has been improved even further. For
|
|
|
|
|
example, there is now practically no overhead for
|
|
|
|
|
tracking derefs in components that don’t use "
|
|
|
|
|
[:code "atom"] "s. Allocations and memory use have also
|
|
|
|
|
been reduced."]
|
|
|
|
|
|
|
|
|
|
[:li "Intro and examples have been tweaked a little to take
|
|
|
|
|
advantage of the new calling conventions."]]
|
|
|
|
|
|
|
|
|
|
[:h2 "New svg example"]
|
|
|
|
|
|
2014-02-15 16:29:37 +00:00
|
|
|
|
[:p "There is also a new, elegant and simple "
|
|
|
|
|
[:a geometry "example"] " of using svg with Reagent, written
|
|
|
|
|
by " [:a jonase "Jonas Enlund"] ". It also shows how you can
|
|
|
|
|
use Reagent’s new calling convensions, and looks like
|
|
|
|
|
this:"]
|
|
|
|
|
|
2014-11-29 17:30:24 +00:00
|
|
|
|
[demo-component {:comp geometry-example}]])]]))
|
2014-02-15 16:29:37 +00:00
|
|
|
|
|
2015-09-08 18:18:27 +00:00
|
|
|
|
(tools/register-page url [#'main] title)
|