reagent/demo/reagentdemo/news/anyargs.cljs

136 lines
5.2 KiB
Plaintext
Raw Normal View History

2014-02-15 16:29:37 +00:00
(ns reagentdemo.news.anyargs
(:require [reagent.core :as r :refer [atom]]
[reagent.interop :refer-macros [.' .!]]
2014-02-15 16:29:37 +00:00
[reagent.debug :refer-macros [dbg println]]
[reagentdemo.syntax :refer-macros [get-source]]
[sitetools :as tools :refer [link]]
2014-02-15 16:29:37 +00:00
[reagentdemo.common :as common :refer [demo-component]]
[geometry.core :as geometry]))
(def url "news/any-arguments.html")
(def title "All arguments allowed")
2014-02-15 16:29:37 +00:00
(def funmap (-> ::this get-source common/fun-map))
(def src-for (partial common/src-for funmap))
(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]}]
(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
[:h1 [link {:href url} title]]
2014-02-15 16:29:37 +00:00
[:div.demo-text
[: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."]
[: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
[: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
[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:"]
[demo-component {:comp say-hello
2014-02-15 16:29:37 +00:00
:src (src-for [:hello-component :say-hello])}]
[:p "In the above example, it wouldnt make any difference at
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 dont change)."]
2014-02-15 16:29:37 +00:00
[:p "But there is one drawback: component function no longer
receives the ”current component” as a parameter. Instead
youll 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 its safest to always
put the call at the top, as in " [:code "my-div"] " here:"]
2014-02-15 16:29:37 +00:00
[demo-component {:comp call-my-div
2014-02-15 16:29:37 +00:00
:src (src-for [:nsr :my-div :call-my-div])}]
[:p [:em "Note: "] [:code "r/props"] " and "
[:code "r/children"] " correspond to Reacts "
[:code "this.props"] " and " [:code "this.props.children"] ",
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."]
[: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 dont 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 Reagents new calling convensions, and looks like
this:"]
[demo-component {:comp geometry-example}]])]]))
2014-02-15 16:29:37 +00:00
(tools/register-page url (fn [] [main])
(str "Reagent 0.4.0: " title))