翻蚳è ããïŒããã¯ãReactã䜿çšããå ç¢ãªWebã¢ããªã®æ§ç¯ã·ãªãŒãºã®æåã®èšäºã§ãã
翻蚳ïŒ
- å ç¢ãªReact Webã¢ããªã±ãŒã·ã§ã³ã®æ§ç¯ïŒããŒã1ããã©ãŠã¶ãŒãããã¿ã€ã
- Trusted React Webã¢ããªã±ãŒã·ã§ã³ã®æ§ç¯ïŒããŒã2ãBrowserifyã«ããæé©å
- Trusted React Webã¢ããªã±ãŒã·ã§ã³ã®æ§ç¯ïŒããŒã3ãJasmineã䜿çšãããã¹ã
- å ç¢ãªReact Webã¢ããªã±ãŒã·ã§ã³ã®æ§ç¯ïŒããŒã4ããµãŒããŒçæ
Webãå®è¡ãããã©ãŠã¶ãŒãšãããã³ã«ãã©ã®ããã«é 眮ãããŠããããèŠããšãå°ãäžå®ã«æããŸãã ããŸãããŸãè¡ããªãããšãããã°ãããŸãè¡ããªãããšãå€ãã®ã§ãå°ãªããšãç§ãã¡ãæžããŠãããã®ããäžè¬ã«æ©èœãããã®ãé©ãã»ã©ã«ãªããŸãã ãããããå¯èœæ§ã«å¯Ÿãããä¿¡é Œæ§ã¯ããã©ãŒã«ããã¬ã©ã³ã¹ãšäžäœäºææ§ã«ç±æ¥ãããããã¯Webã¹ã¿ãã¯ã®äž»èŠéšåã«æ ¹ãããŠããŸãã ãã©ãŠã¶ã¯ãäŸåé¢ä¿ãååŸã§ããªããã10幎åã«äœæãããäžååãªææžã®è§£æã§ãã£ãŠããæçšãªãã®ã衚瀺ããããã«å¯èœãªéããã¹ãŠãå®è¡ããŸãã
404PageFoundã«ã¯ã1993幎ã«äœæããããŸã æ©èœããŠãããµã€ããå«ãŸããŠããŸãã
ãŠã§ãã®ãã®åºæ¬çãªç¹æ§ã¯å¿ããããŠããããã«æããŸãã éçºè ãšããŠãç§ãã¡ã¯ä»ã®ãã©ãããã©ãŒã ã«ãã£ãŠèšå®ãããæåŸ ã«ãŸããŸãå¿ããããåªåããŠããŸãã ç¹ã«ã¢ãã€ã«éçºè ã®äœæ¥ç°å¢ã«jeããŠããŸãããŸããåçã§æè»ãªã¢ããªã±ãŒã·ã§ã³ãæ§ç¯ããããšèããŠããŸãã ä»ãç§ãã¡ããããè¡ãããšãã§ããã¢ããªã±ãŒã·ã§ã³ãäœæããŠããé©ãã»ã©è³¢ã人ãããããããŸãããããŠãããã¯æ¬åœã«ãã®æ¥çã§åãçŽ æŽãããæéã§ãã ãã ããçŸåšã®ããŒã«ããã¯ã¹ã¯èããŠããŸããã AngularãEmberãKnockoutããã®ä»ããã¯ãæ¬è³ªçã«ä¿¡é Œæ§ãäœãããããã©ãŠã¶ãŒéçºã®æªæ¥ã§ãã
JavaScriptãçŸåšããã³å°æ¥ã®ã³ã³ãã³ãã®æäœã®åºç€ãšãããµã€ããèŠãŠããã®ã§ã¯ãªãããšå¿é ããŠããŸãã ããã¯ãã¹ã¿ãã¯ã®æãè匱ãªéšåã«åºç€ãæ§ç¯ããŠããŸãã-ãžã§ã¬ããŒããŒã¹ã æéïŒFull Frontal 2013ïŒ
ãã©ãŠã¶ã«è¿œå ããè€éããšè² è·ã¯ãæå°éã®åé¡ãçºçããŠããŠãŒã¶ãŒã«ã¯äœã衚瀺ãããªãããšãæå³ããŸãã ããã¯ãåè¿°ã®ããŒã«ã®åé¡ã§ã¯ãªããWebãµã€ããæ§ç¯ããããã®ææ°ã®ã¢ãããŒãã®åé¡ã§ãã 空ã®bodyã¿ã°ã¯ããŒãžã§èš±å¯ãããŠããŸãã ã ç§ãã¡ã®ãµã€ãã¯ãå®ã£ãœãæ¥ç¶ã䜿çšããŠèŠæ±ãããå ŽåããŸãã¯å€ããã©ãŠã¶ã§å®è¡ãããå Žåã«å°ãªããšãã¢ã¯ã»ã¹ã§ããå¿ èŠããããŸãã ç§ãã¡ã¯å¶åŸ¡ã§ããªããã®ãéåžžã«å€ãã®ã§ããŠã§ããã©ãŠã¶ãã¹ããŒããã©ã³ã®äœæ¥ç°å¢ã§ã¯ãªããšããäºå®ãèªèããªããã°ãªããŸããïŒ
SquarespaceããŒã ããŒãžã¯ãJavaScriptã䜿çšããŠãã®å¯èŠæ§ãåŠçããããšã«ããããã¹ãŠã®ã³ã³ãã³ããé ããŸãã
ç§ã¯Ludithã®å²åŠã説ãããã«ããã«ããããã§ã¯ãããŸãããçŽç²ãªHTMLã§ããŒãžãéçºãã¹ãã ãšã¯èšããããããŸããã ãµã€ãã«å°å ¥ããè匱æ§ã¯ç¢ºå®ã«å æã§ããŸããããããè¡ãã«ã¯ã䜿çšãããã¯ããã¯ã®äžéšãåèããå¿ èŠããããŸããããã€ãã®èå³æ·±ãããŒã«ããããããç§ã®ãæ°ã«å ¥ãã¯FacebookãšInstagramã®éçºè ã®Reactã©ã€ãã©ãªã§ãã
Reactã¯ãã€ã³ã¿ã©ã¯ãã£ããªã€ã³ã¿ãŒãã§ã€ã¹ãæ§ç¯ããã³æäŸããæ°ããæ¹æ³ãæäŸãããããçŽ æŽãããã§ãã Reactã¯ã ã¬ã¹ãã³ã·ããªãã€ããªãããŸãã¯å圢㮠Webã¢ããªã±ãŒã·ã§ã³ãæ§ç¯ããããã®ã·ã³ãã«ãªããŒã«ãæäŸããŸãã ãã©ãŠã¶ãããŒãããåŸãReactãã€ã³ããªãžã§ã³ãã«è¡šç€ºãããããã«ãæåã®HTMLããµãŒããŒäžã§çæããããšãã§ããŸãïŒãã¬ãŒã³ã¬ã¹ãªãã©ãŠã¶ãŒè©æ¬ºãªãïŒã
Reactã¯MVã®ãããªãã¬ãŒã ã¯ãŒã¯ã§ã¯ãªããããã¬ãŒã³ããŒã·ã§ã³ãã®ã¿ãåŠçããããªãã¿ã®ãã¿ãŒã³ã¯åŠçããŸããã Reactãã¥ãŒã¯ãããŒãžã«ãã³ãããå¿ èŠãããåãªãããã¹ãã®æçã§ã¯ãªãã軜éã®äžéDOMãã¥ãŒã§ããããä»®æ³DOMããšããŠããäžè¬çã«ç¥ãããŠããææ³ã§ãã
ããã¥ã¡ã³ãã®çŸåšã®ã¢ãã«ãèªã¿èŸŒãã§å€æŽãã代ããã«ãäžéäœã䜿çšãããšãå·®åã¢ã«ãŽãªãºã ãç¶æ ãåæç»ããããã«å¿ èŠãªæå°éã®æé ãèšç®ã§ããŸãã ããã¯ãè³¢æãªã€ãã³ãå§ä»»ãDOMãããæŽæ°ãªã©ã®ä»ã®ããã©ãŒãã³ã¹ãåäžããããã®ãšçµã¿åãããŠãReactãé©ãã»ã©é«éã«ããŸãã
Reactã䜿çšãããšãããã©ã«ãã§ã¢ããªã±ãŒã·ã§ã³ãè¡šçŸåè±ãã«ã€ã³ã¿ã©ã¯ãã£ãã«èšè¿°ã§ããŸãããåçãªWebãµã€ãã«ä¿¡é Œæ§ãæ»ããŸãã ããã¯åççã§ãããããã©ãŒãã³ã¹ãå°ãããšããŠããããã«ãªããŸãã
å¿çšäŸ
ç§ãäœæããäŸã¯ãTrackerNet APIãŸãã¯ç¥ããŠãTube Trackerãã䜿çšãããã³ãã³å°äžéé§ ã®ãã£ã¹ãããããŒãã§ãã
ãã®ã¢ããªã±ãŒã·ã§ã³ã«ã¯ãæšæ¶ãèªã¿èŸŒã¿ãŸãã¯ãšã©ãŒã¡ãã»ãŒãžã®è¡šç€ºãããã³é§ ããåè»ãéä¿¡ãã衚瀺ã®4ã€ã®ç¶æ ãããããŸããã ãããã¯ã·ã§ã³ã¬ãã«ã®èªå®éçºè ã®å Žåãã¢ããªã±ãŒã·ã§ã³ã¯æ¬¡ã®å³ã§è¡šãããšãã§ããŸãã
ã¢ããªã±ãŒã·ã§ã³ã1è¡ãã€èšè¿°ããããã»ã¹ãå®è¡ãã代ããã«ãReactãåããŠäœ¿çšãããšãã«åœ¹ç«ã€ããã€ãã®åºæ¬çãªããšãšããã»ã¹ã説æããŸãã
TrackerNet APIã¯éå»ã®æ代ã®è£œåã§ãããææžåãäžååã§ãããé§ ãæ¯åºå šäœã®ããŒã¿ãäžè¶³ããŠããŸãã TrackerNetã®åé¡ã¯Chris Applegateã«ãã£ãŠãã説æãããŠããã圌ã®ãªãŒãã³ãœãŒã¹ãããžã§ã¯ãWhen's My Transportã§è§£æ±ºãããŸããã 幞ããªããšã«ããã®ã¿ããŒãããAPI ã¯éããªã眮ãæããããããã§ãã
ãã©ãŠã¶ã®é«éãããã¿ã€ãã³ã°
Reactã¢ããªã±ãŒã·ã§ã³ã¯ãæºåãªãã§ãã©ãŠã¶ãŒã§ãã°ãããããã¿ã€ãåã§ããŸãã ç§ã¯ããªãã·ã§ã³ã®JSXæ§æã䜿çšããŠã³ã³ããŒãã³ããäœæããããšã奜ã¿ãŸããããã«ãããHTMLæ§æã«éåžžã«ãã䌌ããã®ãäœæã§ããŸãã HTMLãJavaScriptã«åã蟌ãã®ãééã£ãŠããããã«æããå Žåãé·å¹Žã®åé¢ãèãããšãããã¯ç解ã§ããŸãããç§ã«ãšã£ãŠã¯ããçç£çãªã¢ãããŒãã§ããããšãããããããã«ããçŽç²ãªJavaScriptã䜿çšãããããã¬ã³ããªã³ã°ã容æã«ãªããŸãã ReactãšJSXãã©ã³ã¹ãã©ãŒããŒïŒFacebook CDNã§å ¥æå¯èœïŒãæ¥ç¶ããã ãã§éå§ã§ããŸãã
<html> <head> <title>My React App</title> <script src="http://fb.me/react-0.9.0.js"></script> <script src="http://fb.me/JSXTransformer-0.9.0.js"></script> </head> <body> <script type="text/jsx"> /** @jsx React.DOM */ </script> </body> </html>
ã¹ã¯ãªããèŠçŽ ã®type
å±æ§ãtext/javascript
以å€ã«èšå®ãããšãJavaScript以å€ã®ããŒã¿ãããŒãžã«åã蟌ãããšãã§ããŸãã scriptèŠçŽ ã¯ãŠãŒã¶ãŒåãã§ã¯ãªããããããŒãžäžã«èŠèŠçãªè¡šç€ºã¯ãããŸããã
ã³ã³ããŒãã³ãéå±€
Reactã¢ããªã±ãŒã·ã§ã³ã¯ãéå±€ã«ç·šæãããã³ã³ããŒãã³ãããçµã¿ç«ãŠãããŸãã ã¢ããªã±ãŒã·ã§ã³ã®ã¢ãŒããã¯ãã£ãèšç»ããæãç°¡åãªæ¹æ³ã¯ãã€ã³ã¿ãŒãã§ã€ã¹ã®åéšåã®è²¬ä»»ãèšç®ãããããã®ãããã¯ãæç»ããããšã§ãã çæ³çã«ã¯ãåã³ã³ããŒãã³ãã¯1ã€ã®ããšã ããåŠçã ãè€éãªã³ã³ããŒãã³ãã¯ããå°ããªãµãã³ã³ããŒãã³ãã«åå²ãããããã«ããŸãã
ã¯ã€ã€ãŒãã¬ãŒã ã¯ã次ã®éå±€ã«ç·šæã§ããŸãã
-
TubeTracker
ã«ã¯ã¢ããªã±ãŒã·ã§ã³ãå«ãŸããŠããŸã
-
Network
ã¯ããããã¯ãŒã¯å ã®åè¡ã衚瀺ããŸã
-
Line
ã¯ãã©ã€ã³äžã®ã¹ããŒã·ã§ã³ã衚瀺ããŸã
-
-
Predictions
ã¯éä¿¡ããŒãã®ã¹ããŒã¿ã¹ãç£èŠããŸã
-
DepartureBoard
çŸåšã®ã¹ããŒã·ã§ã³ãšãã©ãããã©ãŒã ã衚瀺ããŸã
-
-
ããããã£ãšæ¡ä»¶
Reactã«ã¯2çš®é¡ã®ããŒã¿ããããŸãã ãããããã£ãã¯ã³ã³ããŒãã³ãéã§æž¡ããããç¶æ ãã¯ã³ã³ããŒãã³ãå ã«ä¿åãããŸãã ã³ã³ããŒãã³ãã¯ç¶æ ãå€æŽã§ããŸããããã®ããããã£ã¯å€æŽãããŸãããããã¯è¯ãããšã§ããçæ³çã«ã¯ãçå®ã®ãœãŒã¹ã¯1ã€ã ãã§ããã¹ãã ããã§ãã Reactã¢ããªã±ãŒã·ã§ã³ãèšèšããéã®éèŠãªã¢ãŒããã¯ãã£äžã®æ±ºå®ã¯ãåã³ã³ããŒãã³ãã«å¿ èŠãªããŒã¿ãšããã®ãœãŒã¹ã®å Žæã決å®ããããšã§ãã
Tube Trackerã«ã¯ããããã¯ãŒã¯ããŒã¿ïŒã©ã€ã³ãã¹ããŒã·ã§ã³ãªã©ïŒããŠãŒã¶ãŒãéžæããã©ã€ã³ãšã¹ããŒã·ã§ã³ãTrackerNetããååŸããã¹ã±ãžã¥ãŒã«ããŒã¿ã®3çš®é¡ã®ããŒã¿ã®ã¿ãå¿ èŠã§ãã
ãããã¯ãŒã¯ããŒã¿ã¯ãã¹ããŒã·ã§ã³ã®ãªã¹ããæäŸãã
Network
ããã³
Line
ã³ã³ããŒãã³ããããã³ãŠãŒã¶ãŒå ¥åãæ€èšŒãã
TubeTracker
ã³ã³ããŒãã³ãã«ãã£ãŠ
TubeTracker
䜿çšãããŸãã ããã¯å€§éã®ããŒã¿ã§ãããããå€éšã§åŠçããŠããã¢ããªã±ãŒã·ã§ã³ã«è»¢éããããšããå§ãããŸãã
ãŠãŒã¶ãŒãéžæããã©ã€ã³ãšã¹ããŒã·ã§ã³ã¯
Predictions
ã³ã³ããŒãã³ãã«ãã£ãŠäœ¿çšãããŸãããéžæãããæ¡ä»¶ã¯
Line
ã³ã³ããŒãã³ãã«ãã£ãŠæäŸãããŸãã ã³ã³ããŒãã³ãã®éå±€ã«åŸã£ãŠããããã®å ±éã®ç¥å ã¯TubeTrackerã§ãããããéžæããã©ã€ã³ãšã¹ããŒã·ã§ã³ã¯
TubeTracker
ã³ã³ããŒãã³ãã®ç¶æ ã§ä¿åããå¿ èŠããããŸãã
æåŸã«ãTrackerNetäºæž¬ããŒã¿ã¯
Trains
ã
DepartureBoard
ããã³
Predictions
ã³ã³ããŒãã³ãã§äœ¿çšãããŸããã
TubeTracker
ã³ã³ããŒãã³ãã§ã¯äœ¿çšãããªãããã
Predictions
ã³ã³ããŒãã³ãã«ä¿åããå¿ èŠããããŸãã
ãã ããåé¡ããããŸããããŒã¿ãéå±€ã«æ»ãå¿ èŠããããŸãã
Line
ã³ã³ããŒãã³ãããã®ç¥å ã
TubeTracker
ããŠãŒã¶ãŒå ¥åãåãåããšã
TubeTracker
ã«ã€ããŠç¥ãå¿ èŠããããŸãã
Reactã§èããïŒReactã§èããïŒ -ã³ã³ããŒãã³ãã®éå±€ãšããããã£ãšç¶æ ã®éãã®ãã詳现ãªæŠèŠã¯ãèªã䟡å€ããããŸãã
ã³ã³ããŒãã³ãéä¿¡
ããŒã¿ã¯éå±€ã®äžã«ã®ã¿è»¢éã§ãããããå¥ã®ææ³ã䜿çšããŠããŒã¿ãæž¡ãå¿ èŠããããŸãã ãããè¡ãã«ã¯ãäž»ã«2ã€ã®æ¹æ³ããããŸãã
ã³ã³ããŒãã³ããçŽæ¥ã®ç¥å ã«ã®ã¿ããŒã¿ãéä¿¡ããå¿ èŠãããå Žåãã³ãŒã«ããã¯ãæäŸããããšãæãç°¡åãªãœãªã¥ãŒã·ã§ã³ã§ãã Reactã¯ã³ã³ããŒãã³ãã¡ãœãããåã€ã³ã¹ã¿ã³ã¹ã«èªåçã«ãã€ã³ããããããã³ã³ããã¹ããæž¡ãããšãå¿é ããå¿ èŠã¯ãããŸããã
var Parent = React.createClass({ handleClick: function(e) {...}, render: function() { return <Child callback={this.handleClick} />; } }); var Child = React.createClass({ render: function() { return <a onClick={this.props.callback}>Click me</a>; } });
ããã«éç¥ããå Žåã ãããªãã·ã¥/ãµãã¹ã¯ã©ã€ãã·ã¹ãã ã®æè»æ§ãé«ãŸããã¡ã³ããã³ã¹ã容æã«ãªããŸãã ããã¯ããã€ãã£ãJavaScriptã€ãã³ããŸãã¯PubSubJSã©ã€ãã©ãªã䜿çšããŠãã³ã³ããŒãã³ãã®ã©ã€ããµã€ã¯ã«ã¡ãœããã«ãã€ã³ãããããšã§å®è¡ã§ããŸãã
var Parent = React.createClass({ handleMyEvent: function(e) {...}, componentWillMount: function() { window.addEventListener("my-event", this.handleMyEvent, false); }, componentWillUnmount: function() { window.removeEventListener("my-event", this.handleMyEvent, false); }, render: function() {...} }); var Grandchild = React.createClass({ handleClick: function(e) { var customEvent = new CustomEvent("my-event", { detail: { ... }, bubbles: true }); this.refs.link.getDOMNode().dispatchEvent(customEvent); }, render: function() { return <a ref="link" onClick={this.handleClick}>Click me</a>; } });
ã³ã³ããŒãã³ãã®ã©ã€ããµã€ã¯ã«
ã³ã³ããŒãã³ãã«ã¯ãã©ã€ããµã€ã¯ã«ãããã¯ããç°¡åãªAPIããããŸãã äœæïŒããŠã³ãïŒãæŽæ°ãããã³ç Žæ£ïŒã¢ã³ããŠã³ãïŒã åçã€ã³ã¿ãŒãã§ã€ã¹ãæ§ç¯ããä»ã®ã¢ãããŒããšã¯ç°ãªãããã®æ©èœã¯ã³ã³ããŒãã³ãã®å®çŸ©ã«çµã¿èŸŒãŸããŠããŸãã ã³ã³ããŒãã³ãéä¿¡ã®äŸã§ã¯ã
componentWillUnmount
ã¡ãœãããš
componentWillUnmount
ã¡ãœããã䜿çšããŠã€ãã³ããã³ãã©ãŒãè¿œå ããã³åé€ããŸããããã³ã³ããŒãã³ãã®ããããã£ãšç¶æ ã詳现ã«å¶åŸ¡ããä»ã®ã¡ãœããããããŸãã Tube Trackerã¢ããªã±ãŒã·ã§ã³ã§ã¯ã次ã®ã¡ãœããã䜿çšããŸããã
-
componentDidMount
ã¯ãã³ã³ããŒãã³ããã¬ã³ããªã³ã°ãããåŸã«åŒã³åºãããŸã;çæãããDOMã«äŸåããä»ã®ã³ãŒããšã®çµ±åãã€ã³ããšããŠåœ¹ç«ã¡ãŸãã -
componentWillReceiveProps
ã¯ãã³ã³ããŒãã³ããæ°ããããããã£ãåãåããã³ã«åŒã³åºãããŸããããã¯ãè¿œå ãããããããã£ã®åœ±é¿ãåããçŸåšå®è¡äžã®æäœããã£ã³ã»ã«ããã®ã«åœ¹ç«ã¡ãŸãã -
shouldComponentUpdate
ç¶æ ãŸãã¯ããããã£ã®å€æŽãåæç»ããå¿ èŠããããã©ãããæåã§å¶åŸ¡ããããã®äŸ¿å©ãªããã¯ã§ãã
ãããç§ãåã«ããããšã§ã
ããã§ããã©ãŠã¶ç°å¢ãã€ã³ã¹ããŒã«ããUIãã³ã³ããŒãã³ãã«åå²ããå¿ èŠãªããŒã¿ãä¿åå Žæãã¢ããªã±ãŒã·ã§ã³å ã§ã®é åžæ¹æ³ã確èªããŸããããã¢ã®æéã§ãã
Expressã䜿çšããŠå°ããªHTTPãµãŒããŒãã€ã³ã¹ããŒã«ããéçãã¡ã€ã«ãå±éããŸãããããã¯ããã®èšäºã·ãªãŒãºã®æ¬¡ã®ããŒãã§äœ¿çšããŸãã ä»ããã¢ããªã±ãŒã·ã§ã³ãè©Šãããšãã§ããŸã ïŒæ³šïŒãµã³ãã«ã¯ç¡æã®ã¢ã«ãŠã³ãã§å®è¡ãããŠããããããã®ãªã³ã¯ã¯äžå®å®ã«ãªãå¯èœæ§ããããŸãïŒããŸãã¯ãGitHubã«ã¢ã¯ã»ã¹ããŠãœãŒã¹ã³ãŒãã確èªããŸã ã
第2éšã§ã¯ãã¢ããªã±ãŒã·ã§ã³ã®æé©åã«ã€ããŠèª¬æããŸãã ãã©ãŠã¶çšã®ã³ãŒãæé©åããŒã«ã®ã€ã³ã¹ããŒã«ã ç§ã«ã³ã¡ã³ããŸãã¯ãã€ãŒãããŠãã ãã ããã£ãŒãããã¯ããåŸ ã¡ããŠãããŸãã