"A JAVASCRIPT LIBRARY FOR BUILDING USER INTERFACES"
http://facebook.github.io/react/Hello World käyttäen JSX:ää
/** @jsx React.DOM */ var HelloMessage = React.createClass({ render: function() { return <div>Hello {this.props.name}</div>; } }); React.renderComponent(<HelloMessage name="John" />, document.getElementById("container"));
JSX ??..
JSX
/** @jsx React.DOM */ var HelloMessage = React.createClass({ render: function() { return <div>Hello {this.props.name}</div>; } });
Javascript
var HelloMessage = React.createClass({ render: function() { return React.DOM.div(null, "Hello ", this.props.name); } });
Virtuaali-DOM esimerkki
var MyComponent = React.createClass({ render: function() { if (this.props.first) { return <div classname="first"><span>A Span</span></div>; } else { return <div classname="second"><p>A Paragraph</p></div>; } } });
Tyhjästä tilanteeseen jossa "first" arvo asetettu
<div className="first"><span>A Span</span></div>
Virtuaali-DOM esimerkki
var MyComponent = React.createClass({ render: function() { if (this.props.first) { return <div classname="first"><span>A Span</span></div>; } else { return <div classname="second"><p>A Paragraph</p></div>; } } });
Tilanteesta "first" tilanteeseen "second" -vaiheet
<span>A Span</span>
<p>A Paragraph</p>
Komponenttien elinkaari
var Component = React.createClass({ componentWillMount : function(){ console.log("componentWillMount") }, render : function(){ console.log("render"); return <p>component</p> }, componentDidMount : function(){ console.log("componentDidMount") } }); React.renderComponent(<Component />, document.body)fiddle
Refs - DOM elementtien käsittely
var Component = React.createClass({ componentWillMount : function(){ console.log("componentWillMount") }, render : function(){ console.log("render"); return <p ref="component">component</p> }, componentDidMount : function(){ console.log("componentDidMount") this.refs.component.getDOMNode().innerHTML="updated after render"; } }); React.renderComponent(<Component />, document.body)fiddle
Komponenttiesimerkki 1 - Kompositio ja propertyt
var Message = React.createClass({ render : function(){ return (<p><b>{this.props.sender}</b> {this.props.message}</p>) } }); var Messages = React.createClass({ render: function() { return (<ul> {this.props.messages.map(function (msg){ return <li><Message sender={msg.sender} message={msg.message} /></li> })} </ul>) } }); var msgs = [{sender: "Doge", message : "Wow, such example"}, {sender: "Swag", message : "#Yolo"}] React.renderComponent(<Messages messages={msgs} />, document.body);fiddle
Komponenttiesimerkki 2 - Tilalliset komponentit ja esimerkkisovellus
fiddleTilalliset komponentit
"Hyödyllinen" Mixin - esimerkki
fiddleReactin käyttö ilman ClojureScript-apukirjastoa
(def comments-data [{:author "Pete Hunt" :text "This is a comment."} {:author "Jordan Walke" :text "This is *another* coment"}]) (def Comment (js/React.createClass #js {:render (fn [] (this-as this (let [comment (.. this -props -comment)] (js/React.DOM.div #js {:className "comment"} (js/React.DOM.h2 nil (:author comment)) (js/React.DOM.span nil (:text comment))))))})) (def CommentList (js/React.createClass #js {:render (fn [] (this-as this (js/React.DOM.div #js {:className "commentList"} (into-array (map #(Comment #js {:comment %}) (.. this -props -comments))))))})) (def CommentBox (js/React.createClass #js {:render (fn [] (this-as this (js/React.DOM.div #js {:className "commentBox"} (js/React.DOM.h1 nil "Comments") (CommentList #js {:comments (.. this -props -comments)}))))})) (js/React.renderComponent (CommentBox #js {:comments comments-data}) (.getElementById js/document "content"))Lähde
Om
(defn comment [{:keys [author text]} c] (om/component (dom/div #js {:className "comment"} (dom/h2 nil author) (dom/span nil text)))) (defn comment-list [app] (om/component (dom/div nil (dom/div #js {:className "commentList"} (into-array (map #(om/build comment app {:path [%]}) (range (count app)))))))) (defn comment-box [app] (om/component (dom/div #js {:className "commentBox"} (dom/h1 nil "Comment") (om/build comment-list app {:path []})))) (om/root comments-data comment-box (.getElementById js/document "content"))Lähde
Reagent (ex-Cloact)
https://github.com/holmsand/reagentHoplon.io