ç§ãã¡ã¯ããªããåã°ããŸã-Reactã©ã€ãã©ãªã«é¢ããæªåé«ãStoyan Stefanovã®æ¬ã¯ãã§ã«å®å šã«ç¿»èš³ãããŠããŸã
倧èŠæš¡ãªJavaScriptãã©ã³ã¯äžã®ãã®è¥ãèœã¯æ éã«å®£äŒããã®ã«åœ¹ç«ã€ãšèãããããã¬ãã¥ãŒãšå°ãç±ççãªèšäºãèªãããšããå§ãããŸãã
Reactã¯ãã§ã«JavaScriptãã¬ãŒã ã¯ãŒã¯ã®äžã§æãææãªäž»äººå ¬ã®1人ã«ãªã£ãŠããŸãã ãã®ããŒã«ã¯ã·ã³ãã«ã§äœ¿ããããã§ãããããã«ãããããããReactã®æçšæ§ã蚌æããã¡ãªãããæ確ã«è¡šçŸããããšã¯éåžžã«å°é£ã§ãã ãããžã§ã¯ãã®èè èªèº«ã§ããããããéçºãå§ããŠã圌ãã¯ãã®ãããªè³ªåã«å šãçããããšãã§ããªãã£ãããšãèªããŸããã
Reactã®ããŒã¯ã«å¯Ÿãã2ã€ã®äž»ãªç°è°ã¯æ¬¡ã®ãšããã§ãã
- ãã®äžå®å šã-Reactã¯ãMeteorãAngularãšã¯ç°ãªããããã«ã¹ã¿ãã¯ããã¬ãŒã ã¯ãŒã¯ã§ã¯ãããŸããã ã«ãŒã¿ãŒãã¢ãã«ã®ã·ã¹ãã ãããã³ãã¬ãŒã ã¯ãŒã¯ãæã£ãŠããã¯ãã®ãã®ä»ã®å€ãã®æ©èœã¯ãããŸããã MVCã®ãVãã®ã¿ãå®è£ ããŠãããšåœŒãã¯èšããŸãããããã¯ç§ã話ãããã±ãŒã¹ãšã¯çšé ãã§ãã
- ãã®æ§æã¯ãããã§ã¯HTMLãšJavascriptãæ··åšããŠããŸãã ããã°ã©ããŒã¯JSXãèŠãŠããã§ã...ãªãïŒããšå°ããŸãã ãŸããCSSããã·ã³ã°ã奜ã人ãããŸãã
ãããã®åé¡ã®äž¡æ¹ã匷調ãããšãšãã«ãReactã®é·æã®ããã€ãã«çŠç¹ãåœãŠãŸãã ãããè¡ããïŒ
ããã§ã¯ãReactãšã¯æ£ç¢ºã«ã¯äœã§ããïŒ
Reactã¯ãã³ã³ããŒãã³ããäœæããããã®JavaScriptã©ã€ãã©ãªã§ãã åã³ã³ããŒãã³ãã«ã¯ãå€èŠ³ãããããããã¯renderã¡ãœãããš
getInitialState
å®çŸ©ãããããã€ãã®ç¶æ ã«ãã£ãŠèšå®ãããŸãã å€èŠ³ã¯çŸåšã®ç¶æ ã«ãã£ãŠç°ãªããŸãã ããã¯æ¬è³ªçã«ããã¹ãŠã§ãã ãããŠããããReactã説æããã®ãéåžžã«é£ããçç±ã§ã-å€ãã®äººã«ãšã£ãŠãããã¯ãŸãã«ãäžå®å šããã®æèŠãåŒãèµ·ãããŸãã
äŸïŒ
Counter = React.createClass getInitialState: -> count: 0 increment: -> @setState(count: @state.count + 1) render: -> <div> { @state.count } <button onClick={ @increment }>+</button> </div>
ãã®äŸã§ã¯ããã¿ã³ããããã¯ãªãã¯ãããšãincrementã¡ãœãããåŒã³åºãããŸãã
increment
ã¡ãœããã¯setStateãåŒã³åºããããçŸåšã®çªå·ããã1ã€å€ãæ°ããçªå·ãèšå®ãããŸãã
setState
ã¡ãœãã
setState
ã³ã³ããŒãã³ããåã³
setState
ãŸãã ããã¯ãReactãšãã®ã¯ããŒã³ãåæ§ã®ããŒã«ãšç°ãªãã®ã¯ãã®ããã§ãã å®éãReactã¯ãä»®æ³DOMããšåŒã°ããããã¥ã¡ã³ãã®ãªããžã§ã¯ãã¢ãã«ãã¡ã¢ãªã«ä¿åãïŒãã·ã£ããŠDOMãã¯ãããã«ç°ãªãçŸè±¡ã§ãïŒããã®DOMãšãã©ãŠã¶ãŒã«è¡šç€ºããããã®ãšã®å·®ãèšç®ããŸãã ãã®éãã«åºã¥ããŠãäœæããå¿ èŠãããå€æŽã®ãªã¹ããäœæãããŸãïŒ
createElement
æ³åããŠãã ããïŒã次ã«ããããã®å€æŽãé©çšãããŸãã
ãã¡ãããReactã§ã¯ãããããã£ïŒå°éå ·ïŒãã¿ã°ã®ããŸããŸãªå±æ§ãªã©ãä»ã®å€ãã®æŠå¿µãç解ããå¿ èŠããããŸãããDOMã¯æãéèŠã§ãã
Reactandãšã¯äœã§ãã
äžå®å šãã è¯ããæªãã
Railsãç»å Žãããšãã圌ãã®ãæºè ¹æããç¹ã«å®£äŒãããŠããŸããïŒãã®ãããªèšèãããã°ããã®ã§ããïŒã ãã以æ¥ãåçã®èªçµŠèªè¶³ã§ããããšãå€æããéžæè¢ã¯ãããŸããããªããªããRailsã«ã¯ããŒã¿ããŒã¹æœè±¡åã®ã¬ãã«ãã«ãŒãã£ã³ã°ã·ã¹ãã ããã³ãã¬ãŒããšã³ãžã³ãªã©ãããããã§ãã
ç§ã®ç¥ãéããæåã®éžæè¢ã¯ãçç±ããã®ãã£ã³ãã§ãã-ã³ãŒãã®èªã¿åãè¡ã§æ§æããããã€ã¯ããã¬ãŒã ã¯ãŒã¯ã ããã«ç¶ããŠãã·ããã©ããã§ãªãŒãã€ããã©ã¹ã³ããšã¯ã¹ãã¬ã¹ãªã©ãç¶ããŸããã ããããå®éã«ã¯ããããã¯ãã¹ãŠãã¬ãŒã ã¯ãŒã¯ã®æŠå¿µããšãã¥ã¬ãŒããç¶ããŠããããããã«èããªã£ãŠããã ãã§ãªããåããå°ãªãããã«æžãããŠããŸãã ãããããããã®ããŒã«ã¯ã倧éšãããŠããªããã販売ãããŠãããæ¯èŒçå°ããªã¢ããªã±ãŒã·ã§ã³ã®éçºã«äŸ¿å©ã§ãã
Reactãåºå¥ããäžå®å šæ§ã¯æ ¹æ¬çã«ç°ãªããŸãã Reactã¯åãªãLite Angularã§ã¯ãªããæ ¹æ¬çã«ç°ãªããã®ã§ãã ããã¯ç¹å®ã®ã¿ã¹ã¯ã®éåžžã«è¯ãä»äºãããŸãïŒããã¯ãŠã§ãã³ã³ããŒãã³ãã§åäœããŸãã ãããã£ãŠããããžã§ã¯ãã®å€§ããã«ã€ããŠè©±ãã®ã¯ééã£ãŠããŸãã ã¡ãªã¿ã«ãReactã¯ããã©ãŒãã³ã¹ã«éç¹ã眮ããŠäœæãããŸãããããã¯ããããå®è¡ããFacebookã³ã¡ã³ãã·ã¹ãã ã§ãããããã£ãŠãReactã¯ééããªã巚倧ãªèŠæš¡ã§äœæ¥ããããšãã§ããŸãã
èããã¹ãããšã¯æ¬¡ã®ãšããã§ããæ¬æ Œçãªãã¬ãŒã ã¯ãŒã¯ãå¿ èŠã§ãããããã«ãReactã«åºã¥ããŠå¿ èŠãªãã¹ãŠã®ã³ã³ããŒãã³ããæŽçã§ããŸããã ãŸãããã·ã³ã€ã³ã¿ãŒãã§ã€ã¹ã®ãµã€ãºã¯æ¬è³ªçã«éèŠã§ã¯ãªãããšã«æ³šæããŠãã ããã 1GBãã¬ãŒã ã¯ãŒã¯ãããŠã³ããŒãããå¿ èŠãããããåºåãæè»ã§é«éãªã¢ããªã±ãŒã·ã§ã³ã§ããå Žåãããã¯åé¡ã§ã¯ãããŸããã äžæ¹ãããã³ããšã³ãã®ãµã€ãºã¯éåžžã«éèŠã§ãã ãã¬ãŒã ã¯ãŒã¯ããã®è¿œå ã®è² è·ã¯ãããã«æçµçµæã«åœ±é¿ããŸãã
Reactã«ã¯ãå®å šãªãã¬ãŒã ã¯ãŒã¯ã®ããã«èŠããåªããæ©èœãä»ã«ããããŸãã Reactã§Backboneã¢ãã«ãæ··åããå€ãã®äŸãç¥ã£ãŠããŸãã ã«ãŒãã£ã³ã°ãè¿œå ããããã®åªããã©ã€ãã©ãªããããŸã ã ããããReactã³ãã¥ããã£ãŒã®äž»ãªææã®1ã€ã¯ãã¡ããFluxã§ã ããã ããããã¯ã¢ããªã±ãŒã·ã§ã³ã¢ãŒããã¯ãã£ãžã®ã¢ãããŒããšãããããããŒã«ãããã§ã¯ãããŸããã
Reactã¯ç¥èªMVCã®åãªãVã§ããïŒ
ã¢ã³ãã¬ã»ã¡ãã€ãã¹ã壮倧ãªãã¬ãŒã³ããŒã·ã§ã³ã§è¿°ã¹ãŠããããã«ãReactã®æ©èœã¯Vã«éå®ããããã®ã§ã¯ãããŸãããäºå®ãReactã³ã³ããŒãã³ãã«ã¯ç¶æ ãããããããã®ãããªã³ã³ããŒãã³ãã¯ããæå³ã§ã¢ãã«ã§ãã Medeirosã匷調ããŠããããã«ãç¶æ ã®å€åã«é¢ããå ¥åã®è¡šç€ºãæäŸãããããåæã«ã³ã³ãããŒã©ãŒã®æ©èœããŸãã¯ãã¥ãŒã®æ©èœãåããŠããŸãã ãŸããã³ã³ããŒãã³ãã¯ä»ã®ã³ã³ããŒãã³ããšæ å ±ã亀æãããããã«äŸåããŸãã ãã®ãããã¢ãã«ãšã³ã³ãããŒã©ãŒã«é©ããã©ã€ãã©ãªãæ¢ãå¿ èŠã¯ãããŸããããã®ãããªæèŠã¯ééã£ãŠããŸãã
ãŸããReactã«ã¯ãããã®ãã¹ãŠãäžå¯æ¬ ãªæŠå¿µïŒã³ã³ããŒãã³ãïŒã®åœ¢ã§å«ãŸããŠããããã責任åæ ãäžååã«å®è£ ããããè€éã§é¢é£æ§ã®é«ãã³ãŒããäœæããããã®æãåºãå¯èœæ§ãéãããŸãã
Medeirosã¯ãMVIã®ååïŒ Model-View-IntentionïŒãæ¡çšããããšã§ãããã®åé¡ã解決ããããšãææ¡ããŠããŸããã€ãŸããWebéçºãžã®ããªãç¬ç¹ãªã¢ãããŒããä¿é²ããŸãã ãã®ã¢ãããŒãã®å®è£ ã®1ã€ã¯LoopãšåŒã°ããŸã ã
HTMLãšJSãæ··åšãããçç± ...äœïŒ CSSã???
10幎åãWebéçºã«ã¯ãHTMLãJSãããã³CSSã®åé¢ãšããç¬èªã®ç¥èãªçããããŸããã 3ã€ã®ãã¯ãããžãŒã¯ãã¹ãŠãåå¥ã®ãã¡ã€ã«ãšãã£ã¬ã¯ããªã«åç¬ã§ååšããå¿ èŠããããŸããã ãããã®ãã¡ã€ã«ã¯ãã€ã³ã¯ã«ãŒãã䜿çšããŠè¡ãããçžäºåç §ã®ã¿ãå¯èœã§ãã
ãã®åé¢ãã©ãã»ã©èª¿åããŠãããã«é¢ä¿ãªããããã¯åãªãæè¡ã®åºå¥ã§ããã責任ã§ã¯ãããŸããã React Castã¯ãã®ã¢ãããŒãã«çåãæ±ããŠãããã³ã³ããŒãã³ãã¬ãã«ã§åé¢ãè¡ãããã¹ãã§ããããšãæ確ã«èŠå®ããŠããŸãã ã³ã³ããŒãã³ãã«ã¯ããŸããŸãªãã¯ãããžãŒãæ··åšãããããšãã§ããŸãããåã³ã³ããŒãã³ãã¯é©åã«ã«ãã»ã«åããã³é©åãããèŠçŽ ã§ãããè«ççã«èŠããè«ççã«æ©èœããŸãã ãã®ãããªæ©èœã®ãã¬ãŒã ã¯ãŒã¯ã«ããŸããŸãªæè¡ãèšèªãé¢äžããŠããå Žåã¯ãããã§ãã
ããã¯ãã¹ãŠæåŠãåŒãèµ·ããå¯èœæ§ããããç§ã®æèŠã§ã¯ãããã¯æãäžåçãªåèŠã«ãã£ãŠèª¬æãããŸãã ã»ãã®æ°æéã§ããªãã¯ããã«æ £ããŸãã ã€ã³ã¿ãŒãããã§èŠã€ããReactã«é¢ããæèŠã¯ãã¹ãŠæ¬è³ªçã«åãã§ãã çªç¶ã2ã€ã®éããŠãããã¡ã€ã«ãé£æ¥ããã¿ãã«ä¿æããå¿ èŠããªããªããçç£æ§ãåçã«åäžããŸãã
CSSãã³ã³ããŒãã³ãé åã«å®å šã«ç§»åããããšãã䞻匵ããŠããŸãã ãã©ã³ãã£ã³ã°ããåã«ããã®ããŒãã«é¢ããã¯ãªã¹ããã¡ãŒãã§ããŠã®èå³æ·±ããã¬ãŒã³ããŒã·ã§ã³ãã芧ãã ããã
åè«
ããã«ã¯ãšã³ã·ã¹ãã ããããŸãã
ç§ã¯ãã®èšäºãæžããŠããŸãããReactã¯GitHubã§Emberãæã¡è² ãããŸããã ããã¯ããã¯ããŒã³ã«å£ããåœç¶ãAngularã§ãããã競åä»ç€Ÿããããã¯ããã«éãæé·ããŸãã
Reactã¯Facebookãããžã§ã¯ãã§ãããFacebookã³ã¡ã³ãã·ã¹ãã ãšã»ãšãã©ã®äŒç€Ÿã®ãããžã§ã¯ããå®è£ ãããŠããŸãã ããã«ãReactã¯PinterestãAirBnBã Khan Academy ããã®ä»å€ãã®ã¹ã¿ãŒãã¢ããã§åºã䜿çšãããŠããŸãã ããã«ãAtomãšãã£ã¿ãŒã¯Reactã®äžã«æ§ç¯ãããŠãããMicrosoftã¯ReactããµããŒãããŠããŸãã
ãããã£ãŠãJavaScriptããŒã«ã®æè¡çå±æã®äž»æµã«ã€ããŠè©±ãããšãã§ããéããReactããã§ã«äž»æµã§ããããšã蚌æããããšããŠããŸãã ããã¯ããããžã§ã¯ãããšã³ã·ã¹ãã ã«æé·ãããã©ããã«é¢ä¿ãªãããããžã§ã¯ãã®ãµããŒãã«ã€ããŠããŸãå¿é ããå¿ èŠããªãããšãæå³ããŸãã Reactã®ã©ã€ãã©ãªã®ã¯ã©ãŠããããããšãæ¢ã«ç¢ºèªã§ããŸã-ä»ã®ãšããããããã®å質ãå€æããã®ã¯é£ããã§ãã
ã³ãŒããå€æãããã
Reactãæ¯æããäž»ãªè°è«ã®1ã€ã¯ããã®è«çã«é¢é£ããŠããŸãã ããã¯éåžžã«çå®ã®å£°æã§ããããã®éã¿ãšéèŠæ§ãéå°è©äŸ¡ãã¹ãã§ã¯ãããŸããã
ç§ã®æèŠã§ã¯ãã³ãŒããå€æããããšã¯ããã®äžã®åŒ±ãéšåãšåŒ·ãéšåãåé¢ã§ããã ãã§ã¯ãããŸããã ããã¯ãåé¡èªèããè«ççã«ãã©ãã§äœãèµ·ãã£ãŠãããïŒã€ãã³ããããŒã¿ã¹ããªãŒã ãªã©ïŒãç解ãããããã®èŠçŽ ãè¿ éã«è©äŸ¡ããæåŸã«åé¡ãæé€ããããšãæå³ããŸãã
èå³æ·±ãã®ã¯ãããã°ã©ã ã®å質ãèãããšãããã°ã®æ°ã ãã§ãªããäžè¬çã«ã¢ãžã£ã€ã«ã§ããã°ããã£ãŠã¯ãªããªãããšããèãã ãã§ãªãããââã°ãä¿®æ£ããã®ã«ã©ããããæéãããããã«ã€ããŠã泚æãæãå¿ èŠããããŸãã ãã®ã¢ã€ãã¢ãç¿åŸããç§ã¯ã1幎åŸã«ããåãé€ãããšãã§ããªãã£ããã°ãæãåºããŸããã ããã¯ã²ã©ãã¡ã¢ãªãªãŒã¯ãåŒãèµ·ãããç¹ã«ããŒã¯è² è·ã§ããã°ã©ã å šäœãå€§å¹ ã«é ãããŸããã ãã°ã®æ°ã ãããé²ãã§ãããšããã®ãããªæ¬ é¥ã¯ã»ãã®äžæ¡ãã«ãªããŸããã決å®èªäœã¯éåžžã«æªããã®ã«ãªããŸãã
ãããã£ãŠããã°ãä¿®æ£ããæéã¯ãã³ãŒããå€æããããšãé£ãããã©ããã瀺ããŸãã ãã°ãä¿®æ£ããã®ã«å€ãã®æéããããå Žåãããã¯éçºè ãåé¡ãçºèŠããåã«ããªã察åŠããªããã°ãªããªãããšãæå³ããŸã-ãããŠãã®éãåæ§ã§ãã
Reactã䜿çšãããšãã³ãŒãã®èª¬æãç°¡åã«ãªããããã€ãã®çç±ãåæã«çºçããŸãã ã¡ã€ã³-DOMã¹ããŒãã¬ã¹ã äŸãšããŠãåŸæ¥ã®jQueryã§ã®åãæ¿ãã®å®è£ æ¹æ³ãæ€èšããŸãã
// html <section> <div class="toggled">HERE I AM</div> <button class="toggler">Toggle!</button> </section> // coffescript $('.toggler').on 'click', -> $('.toggled').toggle()
äžèŠããã¹ãŠãã·ã³ãã«ã§ãã ããããããã§åé¡ããããŸããdivã衚瀺ãããŠãããã©ãããã©ã®ããã«ç¢ºèªããŸããïŒ ã¯ããèšãããšãã§ããŸãïŒããã¿ã³ãã¿ã³ãå¶æ°åã¯ãªãã¯ãããšããã¿ã³ã衚瀺ãããããã§ãªãå Žåã¯è¡šç€ºãããŸãããã
Toggler = React.createClass getInitialState: -> visible: true toggle: -> @setState(visible: !@state.visible) render: -> <section> { @state.visible && <div>HERE I AM</div> } <button onClick={ @toggle }>Toggle!</button> </section>
ãããŠä»ãç¶æ³ã¯æ ¹æ¬çã«å€åããŠããŸãã ããªãã¯èšãããšãã§ããŸãïŒããããèŠãããªãã°ãããã¯è¡šç€ºãããŸããã éåïŒããªãã®ã³ãŒããç解ãããããªãããã®ã³ãŒãã§äœããè¡šçŸããããšã¯ã¯ããã«æ確ã«ãªããŸãã
åå©çš
ããã¯éæ³ã®èšèã§ãã ããã¯ãšã³ãã®äžçã§ã¯ãã³ãŒããã©ã°ã¡ã³ãã®åå©çšãçŸå®ã§ãã ããšãã°ãå®ç³ãåµãªã©ã§ãã ããããã¯ã©ã€ã¢ã³ãåŽã§ã¯ãæ°ãããããžã§ã¯ãããšã«ãã¹ãŠãå床çµã¿ç«ãŠãå¿ èŠãããããã«èŠãããããããŸããã å®éãDOMãã€ãã¹ããŒã«ãURLããŒãµãŒãªã©ãåå©çšããã®ã¯ç°¡åã§ãããã³ã³ãã³ãèªäœã«é¢ããŠã¯ãåãããžã§ã¯ããç°ãªã£ãŠèŠãå§ããã¬ã€ã¢ãŠãã®åèŠçŽ ã¯ç¬åµæ§ã§ãããã1ã€ã®ãµã€ãºã«ãã¹ãŠé©åãããŸããã
ãããããžã§ã¯ãããå¥ã®ãããžã§ã¯ãã«ã³ã³ããŒãã³ãã移è¡ããã®ãèŠãããšã¯ãããŸããããéç¥ããã¿ã³ãããã«èšäºããªã¹ããªã©ã®ãååããŸãã¯ããªãŒã¬ããã¯ããªãã®ãå«ããŠã£ã³ããŠã®ãããªã¢ãããã¯ãšã³ãã£ãã£ãååšããçç±ã¯ããããŸããèŠèŠçãªãã¶ã€ã³ãããŒã¯ã¢ãããåäœããåé¢ããå Žåãã¿ã¹ã¯ãããã®ãŸãŸã䜿çšããããšã¯ã§ããŸããã
ç°ãªãäŒæ¥ã§åãéçºãç¹°ãè¿ã䜿çšãããã³ã³ããŒãã³ããå®è£ ãããç¡æã®ã©ã€ãã©ãªã人ã ãã©ã®ããã«æžãããèŠãã®ã¯èå³æ·±ãã§ãããã è¯ãäŸã¯MaterialUIã§ãã
ãããžã§ã¯ãã®æé·
ç§ãåãçµãã ã¯ã©ã€ã¢ã³ãåŽã®ãããžã§ã¯ãã®å€ãã¯ãåœåéåžžã«å°èŠæš¡ã§ãããããã®åŸå€§å¹ ã«æé·ããŸããã ããã€ãã¯ãªãã¡ã¯ã¿ãªã³ã°ãçµãŠå®å šã«ç°ãªãæ§é ãç²åŸããããããæç¹ã§ããã®äžã«å³èæ§é ãå°å ¥ãããŸãããããã¯ãå¢å€§ããè€éãã«å¯ŸåŠããã®ã«åœ¹ç«ã€ãšæãããŸããã ã©ã¡ãã®å Žåãããã®ãããªäœæ¥ã«ã¯å€ãã®æéãšåŽåãå¿ èŠã§ã-ãã ããéèŠãªãªãã¡ã¯ã¿ãªã³ã°ãšåæ§ã«ãéçºãå€§å¹ ã«ã¹ããŒãã¢ããã§ããŸãã
ã³ã³ããŒãã³ãããŒã¹ã®éçºã®éèŠãªç¹åŸŽã®1ã€ã¯ããããžã§ã¯ãã®ãããã®æ®µéãšéåžžã«ææ©çã«èª¿åããŠããããšã§ãã ã³ã³ããŒãã³ãã®å°ããªã»ãããšãã¡ããšæŽçãããããŒã¿ãããŒã§éå§ããããããžã§ã¯ããããã®ãããªæ ¹æ¬çãªå€æŽãå¿ èŠãšããããšã¯æ³åã§ããŸããããã®ãã¹ãŠã®éšåã¯éåžžã«å®å®ããŠããŸãã
以äžã§ãã å®éãããã§ã¯Fluxãããã¯ã«ã€ããŠã¯ã»ãšãã©è§ŠããŸããã§ããããããã¯ãã§ã«å®å šã«ç°ãªãèšäºã®çŽ æã§ãã