ãã®ã³ãŒã¹ã¯ã圌ããèšãããã«ãåå¿ããæ¹æ³ãç¥ããªããããåŠã³ãããã¹ãŠã®äººã«åœ¹ç«ã€ãšä¿¡ããŠããŸãã åæã«ããã®ã³ãŒã¹ãå®æåè¡ç©ã«ããã«ã¯ããªãã®åŽåãšæéãããããŸãããããã£ãŠããã®ãããžã§ã¯ãã®ç«ã¡äžãã«é¢ããæçµæ±ºå®ãäžãåã«ãå šå¡ã«ã³ãŒã¹ãè©äŸ¡ãã翻蚳ã®å®çŸå¯èœæ§ã«é¢ãã調æ»ã«åå ãããããé¡ãããŸãã
ã¬ãã¹ã³1.ã³ãŒã¹ã®æŠèŠãšãã®éçºã«é¢ããæšå¥šäºé
â ãªãªãžãã«
React Fundamentalsã³ãŒã¹ãžããããã ããã»ãžããŒã«ãšç³ããŸããäžçã§æã人æ°ã®ããWebãã¬ãŒã ã¯ãŒã¯ã®1ã€ã䜿çšããŠãããã³ããšã³ããããžã§ã¯ããäœæããæ¹æ³ã«ã€ããŠèª¬æããŸãã ç§ã¯é·å¹Žã³ã³ãã¥ãŒã¿ãŒæè²ã®åéã§åããŠããŸããããç¹ã«çŸåšã V Schoolã§æè²ããã»ã¹ã®çµç¹ãæ åœããŠããŸãã
courseã³ãŒã¹éçºããã»ã¹ã«ã€ããŠ
è€éãªããšããã°ããç¿åŸããããã¹ãŠã®äººãæ¯æŽããããšãç®çãšãããã¬ãŒãã³ã°ã³ãŒã¹ãé·å¹Žã«ããã£ãŠéçºããŠããŸããããç§ã¯ç¬èªã®åŠç¿ã¢ãããŒããéçºããŸããã
ãããããäœããåŠã¶æãç°¡åã§å¹æçãªæ¹æ³ã¯ãç·Žç¿ããæéãšåŽåãspareããŸãªãããšã§ãã ããã°ã©ãã³ã°ã®æ¹æ³ãåŠã³ããå Žåã¯ãèªåã§äœããæ©ãéå§ããé »ç¹ã«è¡ãã»ã©ãæåããå¯èœæ§ãé«ããªããŸãã
éåžžãV Schoolã®çåŸã玹ä»ãããšããç§ã¯èªåã®äººçãã次ã®äŸãæããŸãã æè¿ãç§ã¯æšæãæ±ãããšã«æ¹ãããŸããã ç§ã¯æ¬ãââèªã¿ãYouTubeã§æ°ãåããªãã»ã©ã®ãããªãèŠãŠãããŒã«ããããŸããã ããããç§ã¯èªåã®æã§ããŒã«ãæã«å ¥ãããŸã§ã䟡å€ã®ããããšã¯äœãã§ããŸããã§ããã ã®ããããšçŽãããã§ã®äœæ¥ã«è²»ãããæéããã£ãã®ã§ãéšåãæ¥çããŠããæ¢ãããã ãã§ãç®æšã«è¿ã¥ãããšãã§ããŸããã å®éã®ãšããããã¹ãŠã®éçºã¯åãååã«åºã¥ããŠããŸãã ReactãåŠã³ããã§ããïŒ ã³ãŒããæžããŸãã
ãã®ã³ãŒã¹ã®ã»ãšãã©ã®ã¯ã©ã¹ã«ã¯æŒç¿ãå«ãŸããŠããŸãã ããªããããããèªåã§ããããšããããšãæåŸ ãããŸãã ç¬ç«ããäœæ¥ã®ã¿ã¹ã¯ã«ç²ŸéããŠããå Žåãããã«ãã®è§£æ±ºçã®èª¬æã«é²ããšãå®éã«ã¯ãReactãåŠç¿ããæãé£ããæ¹æ³ãéžæããŸãã ããã«ãç·Žç¿ã«æåŸ ããããŸã§åŸ ã£ãŠã¯ãããŸãããçå ããŠãåŠãã ããšãã¹ãŠãè©ŠããŠãã ããã å¯èœãªéãèªåã§ã³ãŒããæäœããããã«ããŠãã ããã ç¹ã«ãReactãå°ããã¹ã¿ãŒããããèå³ã®ãããã®ãäœæããäœéšããããã¹ãŠã®ããšãäœéšããŠãã ããã ããã«ãããã ãã¥ãŒããªã¢ã«å°ç ããªã©ã®ãã©ãã«ãåé¿ã§ããŸãã
ç§ã®ã¢ãããŒãã®ãã1ã€ã®éèŠãªç¹ã¯ãã€ã³ã¿ãŒãã«ãã¬ãŒãã³ã°ãšå¯Ÿè±¡ãšãªãææã®ç¹°ãè¿ãã§ãã ãããã¯ãåŠãã§ããããšãæ¬åœã«æãåºãããšãã§ããæãéèŠãªããšã§ãã ã³ãŒã¹ã®åŠç¿ã«çã£åããé§ã蟌ãããšã¯æšå¥šãããŸããã ããããŸãã«ããã¥ãŒããªã¢ã«ã®å°çããžã®éã§ãã ãã®ã¢ãããŒãã䜿çšãããšãå®éã«äœããåŠãã ãšããæèŠãåŸãããŸãããå®éã«ã¯ãåã«ãåŠç¿ãããããšãæãåºããŸããã
ãããã£ãŠãææã移åãããšãã¯ãé »ç¹ã«äŒæ©ãåã£ãŠãã ããã ããã¯ã5ã10åéã®å®æçãªäŒæ©ã§ã¯ãªãããã£ãšéå¿çãªãã®ã§ãã ããã€ãã®ååãåŠã³ããããã䜿çšããŠç·Žç¿ãã1æ¥äŒæ©ããŸãã ã³ãŒã¹ã«æ»ã£ããããã§ã«åŠç¿ããè³æãç¹°ãè¿ãããšãéåžžã«åœ¹ç«ã¡ãŸãã ãã¡ããããã®ã¢ãããŒãã§ã¯ãã³ãŒã¹ããã¹ã¿ãŒããã®ã«æéãããããŸãããããã¯åŠç¿ã«éåžžã«æçãªå¹æããããããŸãã
ãã®ã³ãŒã¹ããã¹ã¿ãŒããŠãåŠãã ããšãç°¡åã«èª¬æããŸãããã
âæ§é ãšåææ¡ä»¶
ã³ãŒã¹ã®äž»ãªãããã¯ã®ãªã¹ãã¯æ¬¡ã®ãšããã§ãã
- ã³ã³ããŒãã³ãã Reactãšããã°ãã³ã³ããŒãã³ãã®æŠå¿µã«ã€ããŠã®è°è«ã¯é¿ããããŸããã Reactã®ã³ã³ããŒãã³ãã¯ãåå©çšã«é©ããHTMLã¹ãããããäœæããããã®äž»èŠãªæ§æèŠçŽ ã§ãã ãããŠãå®éã«ç§ãã¡ã話ãä»ã®ãã¹ãŠã¯ããããã®ã³ã³ããŒãã³ãã䜿çšããŠWebã¢ããªã±ãŒã·ã§ã³ãæ§ç¯ããæ¹æ³ã«é¢é£ããŠããŸãã
- JSXã ããã¯ãHTMLããã³JavaScriptã®æ©èœã䜿çšããŠã³ã³ããŒãã³ããäœæã§ããJavaScriptæ§æã®æ¡åŒµæ©èœã§ãã
- ã³ã³ããŒãã³ãã®æ§åŒã ã¹ã¿ã€ãªã³ã°ã«ãããã³ã³ããŒãã³ãã«é åçãªå€èŠ³ãäžããããšãã§ããŸãã
- ã¢ããªã±ãŒã·ã§ã³ã®ããããã£ãšããŒã¿äº€æã ããããã£ã¯ãã³ã³ããŒãã³ãéã§ããŒã¿ã転éããããã«äœ¿çšãããŸãã
- ç¶æ ã ã³ã³ããŒãã³ãç¶æ ã¡ã«ããºã ã¯ãã¢ããªã±ãŒã·ã§ã³ã§ããŒã¿ãä¿åããã³ç®¡çããããã«äœ¿çšãããŸãã
- ã€ãã³ãåŠçã ã€ãã³ãã䜿çšãããšãã¢ããªã±ãŒã·ã§ã³ãŠãŒã¶ãŒãšã€ã³ã¿ã©ã¯ãã£ããªé¢ä¿ã確ç«ã§ããŸãã
- ã³ã³ããŒãã³ãã®ã©ã€ããµã€ã¯ã«ã¡ãœããã ãããã®ã¡ãœããã«ãããããã°ã©ãã¯ã³ã³ããŒãã³ãã§çºçããããŸããŸãªã€ãã³ãã«åœ±é¿ãäžããããšãã§ããŸãã
- HTTPãããã³ã«ã䜿çšããŠå€éšAPIããããŒã¿ãããŠã³ããŒãããŸãã
- ãã©ãŒã ãæäœããŸãã
ãã®ã³ãŒã¹ã§çç£æ§ãçºæ®ããã«ã¯ãHTMLãCSSãããã³JavaScriptïŒES6ïŒãç¥ã£ãŠããå¿ èŠããããŸãã
ã¬ãã¹ã³2.ãã¬ãŒãã³ã°ãããžã§ã¯ã
â ãªãªãžãã«
ãã®ã³ãŒã¹ãå®äºããéçšã§ãæè²ãããžã§ã¯ããéçºããŸãã ãããã®ããã€ããèŠãŠã¿ãŸãããã
æåã®éçºã¯ãæšæºã®TODOã¢ããªã±ãŒã·ã§ã³ã«ãªããŸãã
Todoã¢ããªã±ãŒã·ã§ã³
ããã¯å°ãéå±ã«èŠãããããããŸãããããã®éçºã®éçšã§å€ãã®æ©äŒãé¢äžããŸããããã«ã€ããŠã¯ã³ãŒã¹ã§èª¬æããŸãã To Doãªã¹ãå ã®é ç®ãã¯ãªãã¯ããŠå®äºããŒã¯ãä»ããå€èŠ³ãã©ã®ããã«å€åãããã確èªã§ããŸãã
TODOã¢ããªã±ãŒã·ã§ã³ã§ããŒã¯ãããã±ãŒã¹
ãããŠããããã³ãŒã¹ãããžã§ã¯ãã§ã-ããŒã ãžã§ãã¬ãŒã¿ãŒã§ãã
ããŒã ãžã§ãã¬ãŒã¿ãŒ
ãã®ã¢ããªã±ãŒã·ã§ã³ã䜿çšããå Žåãããã¹ãã¯ã
Top Text
Bottom Text
ãã£ãŒã«ããš
Bottom Text
ãã£ãŒã«ãã«å ¥åããããããããç»åã®äžéšãšäžéšã«åé¡ãããŸãã
Gen
ãã¿ã³ãã¯ãªãã¯ãããšãããã°ã©ã ã¯å¯Ÿå¿ããAPIããããŒã ç»åãã©ã³ãã ã«éžæããããã«ããã¹ããè¿œå ããŸãã ãã®ã¢ããªã±ãŒã·ã§ã³ã®ä»çµã¿ã®äŸã次ã«ç€ºããŸãã
æºåå®äº
ã¬ãã¹ã³3.ãªãåå¿ããã®ãããªãåŠç¿ã«å€ããã®ã
â ãªãªãžãã«
çŽç²ãªJavaScriptã§Webãããžã§ã¯ããéçºã§ããã®ã«ããªãReactã䜿çšããã®ã§ããïŒ Webéçºã«èå³ãããå ŽåãReactã䜿çšãããšãJavaScriptã®ã¿ã䜿çšããŠéæå¯èœãªããã©ãŒãã³ã¹ãè¶ ããéåžžã«é«éãªã¢ããªã±ãŒã·ã§ã³ãäœæã§ãããšèããããšããããããããŸããã ããã¯ãReactã§Virtual DOMãšåŒã°ãããã¯ãããžãŒã䜿çšããŠå®çŸãããŸãã Virtual DOMã®è©³çŽ°ã«ã€ããŠã¯èª¬æããŸããããã®ãã¯ãããžã«ã€ããŠè©³ããç¥ãããå Žåã¯ã ãã®ãããªãã芧ãã ããã
ä»ã®ãšãããVirtual DOMã䜿çšãããšãéåžžã®JSã䜿çšããŠããå Žåãããã¯ããã«é«éã«Webã¢ããªã±ãŒã·ã§ã³ãå®è¡ã§ããããšãããããŸãã ReactãæäŸãããã1ã€ã®å€§ããªå©ç¹ã¯ãåå©çšã«é©ããWebã³ã³ããŒãã³ããäœæã§ããããšã§ãã äŸãèããŠã¿ãŸãããã
Bootstrapã©ã€ãã©ãªã®æšæº
navbar
èŠçŽ ããããŸãã
ããã²ãŒã·ã§ã³ããŒ
Bootstrapã以åã«äœ¿çšããããšããªãå Žåãããã¯Webéçºè ã«çŸããèšèšãããèŠçŽ ãæäŸããåãªãCSSã©ã€ãã©ãªã§ããããšã«æ³šæããŠãã ããã çŽ40è¡ã®ã³ãŒãããããŸããããã¹ãŠé¢åã§ããã®ãããªã³ãŒãå ã移åããã®ã¯ç°¡åã§ã¯ãããŸããã ããããã¹ãŠããã§ã«å€ãã®ãã®ãããHTMLããŒãžã«å«ãããšããã®ãããªããŒãžã®ã³ãŒãã¯ããŸããŸãªãã¶ã€ã³ã§åçŽã«ãªãŒããŒããŒããããŸãã
React Webã³ã³ããŒãã³ãã䜿çšãããšãHTMLã³ãŒãã®ã¹ãããããååŸããŠåå¥ã®ã³ã³ããŒãã³ããšããŠé 眮ãããããã®ã¹ãããããããŒãžã«è¿œå ãã代ããã«ãããŒãžæ§é ã«ç¹å¥ãªHTMLã¿ã°ãªã©ãå«ããããšãã§ããŸãã ãã®å Žåã40è¡ã®HTMLããŒã¯ã¢ãããããŒãžã«è¿œå ãã代ããã«ããã®ããŒã¯ã¢ãããå«ãã³ã³ããŒãã³ããæ§æã«å«ããã ãã§ååã§ãã
MySweetNavbar
ãšåŒã°ã
MySweetNavbar
ã
Webãã©ãŒãã³ã°ãžã®ã³ã³ããŒãã³ãã¢ãããŒã
ãã®ãããªã³ã³ããŒãã³ãã«ã¯ã奜ããªååãä»ããããšãã§ããŸãã ã芧ã®ãšãããã³ã³ããŒãã³ãããŒã¹ã®ããŒãžã¬ã€ã¢ãŠãã¯èªã¿ããããªã£ãŠããŸãã éçºè ã¯ãã®ãããªããŒãžã®å šäœçãªæ§é ãããã«èŠãããšãã§ããŸãã ãã®å Žåã
<body>
ã³ã³ãã³ããããããããã«ãããŒãžäžéšã«ããã²ãŒã·ã§ã³ããŒïŒ
MySweetNavbar
ïŒãäžå€®ã«ã¡ã€ã³ã³ã³ãã³ãïŒ
MainContent
ïŒãããŒãžäžéšã«ããã¿ãŒïŒ
MySweetFooter
ïŒããããŸãã
ããã«ãã³ã³ããŒãã³ãã¯HTMLããŒãžã®ã³ãŒãæ§é ãæ¹åããã ãã§ã¯ãããŸããã ãŸããåå©çšã«ãé©ããŠããŸãã è€æ°ã®ããŒãžã«åãããã²ãŒã·ã§ã³ããŒãå¿ èŠãªå Žåã¯ã©ããªããŸããïŒ ç°ãªãããŒãžã®ãã®ãããªããã«ãäºãã«ãããã«ç°ãªãå Žåã¯ã©ããªããŸããïŒ ãã¹ãŠã®åãããã«ãå€ãã®ããŒãžã§äœ¿çšãããŠãããããã«äœããã®å€æŽãå ããå¿ èŠãããå Žåã¯ã©ãããŸããïŒ ã³ã³ããŒãã³ãã¢ãããŒãã䜿çšããªããšããããã®è³ªåãä»ã®å€ãã®è³ªåã«é©åãªçããåºãããšã¯å°é£ã§ãã
Reactã®äººæ°ã®ãã1ã€ã®çç±ã¯ãFacebookããã®ã©ã€ãã©ãªãéçºããã³ãµããŒãããŠãããšããäºå®ãšèããããšãã§ããŸãã ããã¯ãå°ãªããšããReactãè³æ Œã®ããããã°ã©ããŒã«ãã£ãŠåžžã«åŠçãããŠããããšãæå³ããŸãã Reactã®äººæ°ãšããããžã§ã¯ããGitHubã§å ¬éãããŠãããªãŒãã³ãœãŒã¹ã§ãããšããäºå®ã¯ãå€ãã®ãµãŒãããŒãã£éçºè ãããââãžã§ã¯ãã«è²¢ç®ããŠããããšãæå³ããŸãã ããããã¹ãŠã«ãããReactã¯ãè¿ãå°æ¥ãçããŠçºå±ãããšèšãããšãã§ããŸãã
Reactã«ã€ããŠãç¹ã«ãã®ã©ã€ãã©ãªã調æ»ãã䟡å€ãããçç±ã«ã€ããŠèšãã°ããã®ã©ã€ãã©ãªã«é¢é£ãã巚倧ãªåŽååžå ŽãæãåºããããåŸãŸããã ä»æ¥ãReactã®ã¹ãã·ã£ãªã¹ãã¯çµ¶ããéèŠããããŸãã ããã³ããšã³ãéçºã§ä»äºãèŠã€ããããšãç®æšã«Reactãå匷ãããªããããã¯ããªããæ£ããè»éã«ä¹ã£ãŠããããšãæå³ããŸãã
ã¬ãã¹ã³4ïŒéçºç°å¢ãReactDOMãJSX
â ãªãªãžãã«
ããã§ã¯ãReactDOMã䜿çšããŠç°¡åãªReactã¢ããªã±ãŒã·ã§ã³ãäœæããæ¹æ³ã«ã€ããŠèª¬æããJSXã®éèŠãªãã€ã³ãã«è§ŠããŸãã ããããã³ãŒãã®äœ¿çšãéå§ããåã«ããã®ã³ãŒããå®è¡ããå Žæã«ã€ããŠè©±ããŸãããã
âéçºç°å¢
Reactã³ãŒããè©Šãã«ã¯ãããããæ¬æ ŒçãªããŒã«ã«éçºç°å¢ããããã€ããã®ãæåã§ãã ãããè¡ãã«ã¯ãæè¿å ¬éãããè³æReact.jsãåç §ã§ããŸãã åå¿è åãã®çŽæçãªã¬ã€ã ãç¹ã«ãReact Application Developmentã®ã»ã¯ã·ã§ã³ãåç §ããŠãã ããã ã€ãŸããå®éšãéå§ããã«ã¯ã
create-react-app
ã䜿çšããŠæ°ããã¢ããªã±ãŒã·ã§ã³ãäœæããããŒã«ã«éçºãµãŒããŒãèµ·åããŠã³ãŒãã®ç·šéãéå§ããå¿ èŠããããŸãã æãç°¡åãªäŸã«ã€ããŠèª¬æããŠããå Žåã¯ããããã®ã³ãŒããæšæºã®
index.js
ãã¡ã€ã«ã«çŽæ¥å ¥åãããã®ã³ãŒããåé€ãããã³ã¡ã³ãã¢ãŠãããããšãã§ããŸãã
create-react-app
ã«ãã£ãŠ
create-react-app
ããããããžã§ã¯ãã®
index.html
ãã¡ã€ã«ã®å 容ã¯ããã®ã³ãŒã¹ã§æäŸããããµã³ãã«ã®å 容ã«å¯Ÿå¿ããŠããŸãã ç¹ã«ãèå¥å
root
æã€
<div>
èŠçŽ ã®ããŒãžäžã®ååšã«ã€ããŠè©±ããŠããã
éåžžã«åçŽãªå®éšã«éåžžé©ããŠããå¥ã®ãªãã·ã§ã³ã¯ãcodepen.ioã®ãããªãªã³ã©ã€ã³ãã©ãããã©ãŒã ã䜿çšããããšã§ãã ããšãã°ãããã¯ãã³ã¢ãã©ã¢ãã®Reactã¢ããªã±ãŒã·ã§ã³ã®ãã¢ãããžã§ã¯ãã§ãã Reactã䜿çšããå®éšçšã«Codepenãããžã§ã¯ããæºåããæ¬è³ªã¯ã
react
ããã³
react-dom
ã©ã€ãã©ãªãããã«æ¥ç¶ããããšã§ãïŒããã¯ãããŒãžäžéšã®
Settings
ãã¿ã³ãã¯ãªãã¯ãã衚瀺ããããŠã£ã³ããŠã®
JavaScript
ã»ã¯ã·ã§ã³ã«ç§»åããŠãçµã¿èŸŒã¿ã®æ€çŽ¢ã·ã¹ãã ãå¿ èŠãªã©ã€ãã©ãªã䜿çšããŠïŒã
å®éšã®ããã«ãScrimbaã®æ©èœã䜿çšãããšäŸ¿å©ã«ãªãå¯èœæ§ããããŸãã ãããè¡ãã«ã¯ã察å¿ããã¬ãã¹ã³ã®ããŒãžãéãã ãã§ãã ãããã®ããŒãžãžã®ãªã³ã¯ã¯ãèŠåºãã®äžã«çªå·ãšè·æ¥åãèšèŒãããŠããŸãã
âæåã®ããã°ã©ã
ãã®äŸã§ã¯ãES6ïŒES2015ïŒã®æ©èœã䜿çšãããããããããããã²ãŒãããããšã匷ããå§ãããŸãã ç¹ã«ã
react
ã©ã€ãã©ãªããããžã§ã¯ãã«ã€ã³ããŒãããããã«ã次ã®æ§æã䜿çšãããŸãã
import React from "react"
ãããã£ãŠã
react-dom
ã©ã€ãã©ãªãã€ã³ããŒãã§ããŸãã
import ReactDOM from "react-dom"
次ã«
render() ReactDOM
ããŠãç»é¢ã«äœãã衚瀺ããŸãã
ReactDOM.render()
create-react-app
ã«ãã£ãŠ
create-react-app
ããããããžã§ã¯ããå®éšã«äœ¿çšããå Žåããã®
index.js
ãã¡ã€ã«ïŒVSCodeã§éãïŒã¯æ¬¡ã®å³ã®ããã«ãªããŸãã
index.jsã«ã³ãŒããå ¥åãã
éçºãµãŒããŒãå®è¡ããŠããŠã
http://localhost:3000/
ããŒãžããã©ãŠã¶ãŒã§éããŠããå Žåããã®ãããª
index.js
ãä¿åãããšãšã©ãŒã¡ãã»ãŒãžãä¿åãããŸãã äœæ¥ã®ãã®æ®µéã§ã¯ã
render()
ã³ãã³ãã衚瀺ãã察象ãšå Žæãã·ã¹ãã ã«ãŸã éç¥ããŠããªããããããã¯å®å šã«æ£åžžã§ãã
å®éãããã§ãä»äœæããã³ãŒããåŠçããŸãã ã€ãŸããããã§ã¯Reactããããžã§ã¯ãã«ã€ã³ããŒãããŠãããReactDOMãã€ã³ããŒãããŠããã®ã©ã€ãã©ãªã®æ©èœã䜿çšããŠç»é¢ã«äœãã衚瀺ã§ããããã«ããŸããã
render()
ã¡ãœããã¯2ã€ã®åŒæ°ãåããŸãã æåã®ãã®ã¯ç§ãã¡ãæã£ãŠæ¥ãããã®ã§ããã2çªç®ã¯ç§ãã¡ãäœããæã£ãŠè¡ãããå Žæã§ãã ãããæ¬äŒŒã³ãŒãã®åœ¢åŒã§èšè¿°ãããŠããå Žåã以äžãååŸãããŸãã
ReactDOM.render( , )
åºåãããã®ã¯ãäœããã®åœ¢ã§HTMLããŒãžã«æ·»ä»ããå¿ èŠããããŸãã èšè¿°ããã³ãŒãã¯ãããŒãžã«è¡šç€ºãããHTMLèŠçŽ ã«å€æãããŸãã
ãã®ããŒãžã®å€èŠ³ã¯æ¬¡ã®ãšããã§ãã
<html> <head> <link rel="stylesheet" href="style.css"> </head> <body> <div id="root"></div> <script src="index.js"></script> </body> </html>
<link>
ã¿ã°ã
<script>
ãªã©ãHTMLããŒãžã®ãã¹ãŠã®åºæ¬èŠçŽ ããããŸãã
create-react-app
ã䜿çš
create-react-app
å Žåã
index.html
ããŒãžã¯å°ãç°ãªããŸãã ç¹ã«ãã³ãŒãã«ã¯import
index.js
ããããŸããã å®éã®ãšããããããžã§ã¯ãããã«ããããšã
index.html
ãš
index.js
æ¥ç¶ãèªåçã«è¡ãããŸã ã
root
èå¥åãæã€
<div>
èŠçŽ ã«æ³šæããŠãã ããã ãã®èŠçŽ ã®éå§ã¿ã°ãšçµäºã¿ã°ã®éã«ãReactã¯äœæãããã¹ãŠãé 眮ããŸãã ãã®èŠçŽ ã¯ãã¢ããªã±ãŒã·ã§ã³å šäœã®ã³ã³ãããšèŠãªãããšãã§ããŸãã
index.js
ãã¡ã€ã«ãš
ReactDOM
render()
ReactDOM
ãšã2çªç®ã®åŒæ°ãã€ãŸãããŒã¿ãåºåããå Žæã¯ãèå¥å
root
æã€
<div>
èŠçŽ ãžã®åç §ã«ãªããŸãã ããã§ã¯ãéåžžã®JavaScriptã䜿çšããŸãããã®åŸãRenderã¡ãœããã®2çªç®ã®åŒæ°ã¯æ¬¡ã®ããã«ãªããŸãã
ReactDOM.render( , document.getElementById("root"))
ãã®ã¢ãããŒãã§ã¯ã
render()
ã¡ãœããã¯æåã®åŒæ°ãåãããããèšè¿°ãããã®ãæå®ãããå Žæã«åºåããŸãã ããã§ã¯ããã®æåã®è°è«ã«åãããããŸãããã åçŽãª
<h1>
èŠçŽ ããå§ããŸãããã ãããŠãéåžžãæåã®ããã°ã©ã ãäœæãããšãã®ããã«ãããã¹ã
Hello world!
è¿œå ããŸã
Hello world!
ïŒ
ReactDOM.render(<h1>Hello world!</h1>, document.getElementById("root"))
ãã©ãŠã¶ããŒãžãæŽæ°ãããšãæå®ããããã¹ããæåã®ã¬ãã«ã®ã¿ã€ãã«ãšããŠè¡šç€ºãããŸãã
æåã®ããã°ã©ã ã®çµæ
ããã§ãJavaScriptã¡ãœããã®åŒæ°ãäºæ³ãããå Žæã«HTMLèŠçŽ ã®èª¬æãé 眮ããçç±ã«ã€ããŠè³ªåããããŸãã çµå±ã
<h1>Hello world!</h1>
ãããªãã®ã«çŽé¢ããŠããJavaScriptã¯ããããããã®åŒã®æåã®æåããããå°ãªããæŒç®åã§ãããšå€æããæããã«å€æ°åã次ã«æ¥ãŠã次ã«ãããå€ããã®æ¯èŒæŒç®åãã JavaScriptã¯ããã®äžé£ã®æåã®HTMLèŠçŽ ãèªèããŸãããèªèãã¹ãã§ã¯ãããŸããã
Reactéçºè ã¯ãã©ã€ãã©ãªã ãã§ãªããJSXãšåŒã°ããç¹å¥ãªèšèªãäœæããŸããã JSXã¯HTMLã®ããªãšãŒã·ã§ã³ã«éåžžã«äŒŒãŠããŸãã ããã«ãã»ãšãã©ãã¹ãŠã®JSXã³ãŒããããã®å©ããåããŠåœ¢æãããHTMLããŒã¯ã¢ãããšã»ãŒå®å šã«äžèŽããããšãããããŸãã ãã¡ãããJSXãšHTMLã«ã¯éãããããŸããããããã«ã€ããŠã¯åŸã ã«èª¬æããŸãã
ããªãåçŽã§çãæ瀺ãå°å ¥ããŸããããReactã®è žã§å®è¡ããããšãå€ãã®èå³æ·±ãããšãèµ·ãããŸãã ãã®ããããã®æé ã¯JavaScriptããŒãžã§ã³ã«å€æãããHTMLã³ãŒããçæãããŸããããã®ããã»ã¹ã®è©³çŽ°ã¯èª¬æããŸããã Reactã©ã€ãã©ãªã¯ãŸãã«JSXã䜿çšããJSXæ§ç¯ç©ãæåŸ ã©ããã«åäœãããããã®ãã®ã§ããããã
react-dom
ã ãã§ãªããããžã§ã¯ãã«ã
react
ããå¿ èŠãããã®ã¯ãã®ããã§ãã ãã®äŸã®
import React from "react"
è¡ãã
import React from "react"
ãåé€ããã¹ã¯ãªãããã¡ã€ã«ãä¿åããŠããŒãžãæŽæ°ãããšããšã©ãŒã¡ãã»ãŒãžã衚瀺ãããŸãã ç¹ã«ã
create-react-app
ã¯ãReactã«ã¢ã¯ã»ã¹ããã«JSXã䜿çšã§ããªãããšã瀺ããŸãïŒ
'React' must be in scope when using JSX react/react-in-jsx-scope
ïŒã
å®éããã®äŸã§ã¯ReactãçŽæ¥äœ¿çšããŠããŸããããJSXãæäœããããã«ã©ã€ãã©ãªã䜿çšãããŠããŸãã
JSXã§äœæ¥ããããšã«ã€ããŠãã1ã€æ³šç®ãããã®ã¯ã次ã®JSXèŠçŽ ã次ã ã«ã¬ã³ããªã³ã°ã§ããªãããšã§ãã
<h1>
èŠçŽ ã®åŸã«
<p>
èŠçŽ ãåºåããå¿ èŠããããšããŸãã ãã®ãã¶ã€ã³ã䜿çšããŠã¿ãŸãããã
ReactDOM.render(<h1>Hello world!</h1><p>This is a paragraph</p>, document.getElementById("root")) //
ãã®åŸãããŒãžãæŽæ°ãããšããšã©ãŒã¡ãã»ãŒãžã衚瀺ãããŸãïŒ
create-react-app
ã§ã¯ã
Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag
ããã«èŠã
Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag
ïŒã ãã®ãšã©ãŒã®æ¬è³ªã¯ããã®ãããªèŠçŽ ãä»ã®èŠçŽ ã§ã©ããããå¿ èŠããããšããããšã§ãã æçµçã«ã¯ã2ã€ã®èŠçŽ ããã¹ãããã1ã€ã®èŠçŽ ã®ããã«ãªããŸãã
ãã®äŸãæ©èœããããã«ãã³ãŒã
<h1>Hello world!</h1><p>This is a paragraph</p>
ãã©ããããŸã
<h1>Hello world!</h1><p>This is a paragraph</p>
<div>
èŠçŽ ã®
<h1>Hello world!</h1><p>This is a paragraph</p>
ã§ãã
ReactDOM.render(<div><h1>Hello world!</h1><p>This is a paragraph</p></div>, document.getElementById("root"))
ããŒãžãæŽæ°ãããšããã¹ãŠãæåŸ ã©ããã«è¡šç€ºãããŸãã
ããŒãžããšã«2ã€ã®HTMLèŠçŽ
JSXã«æ £ããã«ã¯æéãããããŸããããã®åŸãæšæºã®JavaScriptããŒã«ã䜿çšããŠHTMLèŠçŽ ãæäœãããããããã®äœ¿çšãã¯ããã«ç°¡åã§äŸ¿å©ã«ãªããŸãã ããšãã°ãæšæºããŒã«ã䜿çšããŠ
<p>
èŠçŽ ãèšè¿°ãããã®ã³ã³ãã³ããæ§æããã«ã¯ã次ã®ãããªãã®ãå¿ èŠã§ãã
var myNewP = document.createElement("p") myNewP.innerHTML = "This is a paragraph."
次ã«ããããæ¢ã«ããŒãžäžã«ããèŠçŽ ã«æ¥ç¶ããå¿ èŠããããŸãã ããã¯åœä»€åããã°ã©ãã³ã°ã®äŸã§ãããJSXã®ãããã§åãããšã宣èšçãªã¹ã¿ã€ã«ã§å®è¡ã§ããŸãã
ã¬ãã¹ã³5.ã¯ãŒã¯ã·ã§ããã ReactDOMãšJSX
â ãªãªãžãã«
ååã®ã¬ãã¹ã³ã§ã¯ãReactDOMãšJSXã«åºäŒããŸãããä»ããã®ç¥èãå®è·µã«ç§»ããŸãã
次ã®ããã«ããã¹ãŠã®å®çšçãªã¿ã¹ã¯ãå®äºããŸãã æåã«ãèŠåºãããã¿ã¹ã¯ãã®ã»ã¯ã·ã§ã³ã§ã¯ãã¿ã¹ã¯èªäœãæäŸãããå Žåã«ãã£ãŠã¯ããã³ããã»ã¯ã·ã§ã³ã§ããã®å®è£ ã«é¢ããç°¡åãªæšå¥šäºé ãæäŸãããŸãã ãã®åŸããœãªã¥ãŒã·ã§ã³ã»ã¯ã·ã§ã³ãç¶ããŸãã èªåã§ã¿ã¹ã¯ãå®äºããããã«ããããåªåãããŠãããäžèšã®ãœãªã¥ãŒã·ã§ã³ã«å¯ŸåŠããããšããå§ãããŸãã
察åŠããŠããªããšæããå Žåã¯ãåã®ã¬ãã¹ã³ã«æ»ããé©åãªè³æãç¹°ãè¿ããŠãããäžåºŠããçŽããŠãã ããã
âãžã§ã
ããŒãžã«ç®æ¡æžããªã¹ãïŒ
<ul>
ïŒã衚瀺ããReactã¢ããªã±ãŒã·ã§ã³ãäœæããŸãã ãã®ãªã¹ãã«ã¯ãããã¹ããå«ã3ã€ã®èŠçŽ ïŒ
<li>
ïŒãå«ãŸããŠããå¿ èŠããããŸãã
âãã³ã
ãŸããå¿ èŠãªã©ã€ãã©ãªããããžã§ã¯ãã«ã€ã³ããŒãããŠããããã®ãã¡ã®1ã€ã䜿çšããŠãããŒãžäžã®JSXã³ãŒãã䜿çšããŠçæãããèŠçŽ ã衚瀺ããŸãã
解決ç
ãŸããå¿
èŠãªã©ã€ãã©ãªããã¡ã€ã«ã«ã€ã³ããŒãããå¿
èŠããããŸãã ã€ãŸãã
ã©ã€ãã©ãªã«ã€ããŠè©±ããŠããã»ããããŒãžäžã«äœãã衚瀺ããããã
ã©ã€ãã©ãªã«ã€ããŠã話ããŸãã
ãã®åŸã
ãªããžã§ã¯ãã®
ã¡ãœããã䜿çšããŠãããŒãžã«è¡šç€ºããèŠçŽ ã®èª¬æãæž¡ãããã®èŠçŽ ã衚瀺ããããŒãžäžã®å Žæã瀺ãå¿ èŠããããŸãã
æåã®åŒæ°ã¯ç®æ¡æžããªã¹ãã®èª¬æã§ããã2çªç®ã¯ããã«è©²åœããããŒãžèŠçŽ ã§ã-èå¥å
æã€
ã§ãã HTMLã³ãŒãã1è¡ã§èšè¿°ã§ããŸããããœãªã¥ãŒã·ã§ã³ã®ããã«èšèšããããšããå§ãããŸãã
å®å šãªãœãªã¥ãŒã·ã§ã³ã³ãŒãã次ã«ç€ºããŸãã
æéã®çµéãšãšãã«ã
ã¡ãœããã䜿çšããŠå€§èŠæš¡ãªæ§é ã
倧éã®HTMLããŒã¯ã¢ããã
æ¹æ³ã«ã€ããŠèª¬æããŸãã
react
ã©ã€ãã©ãªã«ã€ããŠè©±ããŠããã»ããããŒãžäžã«äœãã衚瀺ããããã
react-dom
ã©ã€ãã©ãªã«ã€ããŠã話ããŸãã
import React from "react" import ReactDOM from "react-dom"
ãã®åŸã
ReactDOM
ãªããžã§ã¯ãã®
render()
ã¡ãœããã䜿çšããŠãããŒãžã«è¡šç€ºããèŠçŽ ã®èª¬æãæž¡ãããã®èŠçŽ ã衚瀺ããããŒãžäžã®å Žæã瀺ãå¿ èŠããããŸãã
ReactDOM.render( <ul> <li>1</li> <li>2</li> <li>3</li> </ul>, document.getElementById("root") )
æåã®åŒæ°ã¯ç®æ¡æžããªã¹ãã®èª¬æã§ããã2çªç®ã¯ããã«è©²åœããããŒãžèŠçŽ ã§ã-èå¥å
root
æã€
<div>
ã§ãã HTMLã³ãŒãã1è¡ã§èšè¿°ã§ããŸããããœãªã¥ãŒã·ã§ã³ã®ããã«èšèšããããšããå§ãããŸãã
å®å šãªãœãªã¥ãŒã·ã§ã³ã³ãŒãã次ã«ç€ºããŸãã
import React from "react" import ReactDOM from "react-dom" ReactDOM.render( <ul> <li>1</li> <li>2</li> <li>3</li> </ul>, document.getElementById("root") )
æéã®çµéãšãšãã«ã
render()
ã¡ãœããã䜿çšããŠå€§èŠæš¡ãªæ§é ã
render()
倧éã®HTMLããŒã¯ã¢ããã
render()
æ¹æ³ã«ã€ããŠèª¬æããŸãã
ãŸãšã
ããŠãReactã³ãŒã¹ã®æåã®ã¬ãã¹ã³ã«æ £ãããã調æ»ã«åå ããŠãã ããã
ãæž èŽããããšãããããŸããïŒ
â ããŒã1ïŒã³ãŒã¹ã®æŠèŠãReactãReactDOMãJSXã®äººæ°ã®çç±
â ããŒã2ïŒæ©èœã³ã³ããŒãã³ã
â ããŒã3ïŒã³ã³ããŒãã³ããã¡ã€ã«ããããžã§ã¯ãæ§é
â ããŒã4ïŒèŠªã³ã³ããŒãã³ããšåã³ã³ããŒãã³ã
â ããŒã5ïŒTODOã¢ããªã±ãŒã·ã§ã³ã®äœæ¥ã®éå§ãã¹ã¿ã€ãªã³ã°ã®åºæ¬
â ããŒã6ïŒã³ãŒã¹ã®äžéšã®æ©èœãJSXããã³JavaScriptã«ã€ããŠ
â ããŒã7ïŒã€ã³ã©ã€ã³ã¹ã¿ã€ã«
â ããŒã8ïŒTODOã¢ããªã±ãŒã·ã§ã³ã®ç¶ç¶çãªäœæ¥ãã³ã³ããŒãã³ãã®ããããã£ã«ç²Ÿé
â ããŒã9ïŒã³ã³ããŒãã³ãã®ããããã£
â ããŒã10ïŒã³ã³ããŒãã³ãã®ããããã£ãšã¹ã¿ã€ã«ã®æäœã«é¢ããã¯ãŒã¯ã·ã§ãã
â ããŒã11ïŒåçããŒã¯ã¢ããçæããã³ãããé åã¡ãœãã
â ããŒã12ïŒã¯ãŒã¯ã·ã§ãããTODOã¢ããªã±ãŒã·ã§ã³ã®äœæ¥ã®ç¬¬3段é
â ããŒã13ïŒã¯ã©ã¹ããŒã¹ã®ã³ã³ããŒãã³ã
â ããŒã14ïŒã¯ã©ã¹ããŒã¹ã®ã³ã³ããŒãã³ããã³ã³ããŒãã³ãã®ã¹ããŒã¿ã¹ã«é¢ããã¯ãŒã¯ã·ã§ãã
â ããŒã15ïŒã³ã³ããŒãã³ããã«ã¹ã¯ãŒã¯ã·ã§ãã
â ããŒã16ïŒTODOã¢ããªã±ãŒã·ã§ã³ã®äœæ¥ã®ç¬¬4段éãã€ãã³ãåŠç
â ããŒã17ïŒTODOã¢ããªã±ãŒã·ã§ã³ã®äœæ¥ã®ç¬¬5段éãã³ã³ããŒãã³ãã®ç¶æ ã®å€æŽ
â ããŒã18ïŒTODOã¢ããªã±ãŒã·ã§ã³ã®äœæ¥ã®6çªç®ã®æ®µé
â ããŒã19ïŒã³ã³ããŒãã³ãã®ã©ã€ããµã€ã¯ã«ã¡ãœãã
ããŒã20ïŒæ¡ä»¶ä»ãã¬ã³ããªã³ã°ã®æåã®ã¬ãã¹ã³
â ããŒã21ïŒæ¡ä»¶ä»ãã¬ã³ããªã³ã°ã«é¢ãã2çªç®ã®ã¬ãã¹ã³ãšã¯ãŒã¯ã·ã§ãã
â ããŒã22ïŒå€éšãœãŒã¹ããããŒã¿ãããŠã³ããŒããããTODOã¢ããªã±ãŒã·ã§ã³ã®äœæ¥ã®ç¬¬7段é
â ããŒã23ïŒãã©ãŒã ã®æäœã«é¢ããæåã®ã¬ãã¹ã³
â ããŒã24ïŒ2çªç®ã®ãã©ãŒã ã¬ãã¹ã³
â ããŒã25ïŒãã©ãŒã ã®æäœã«é¢ããã¯ãŒã¯ã·ã§ãã
â ããŒã26ïŒã¢ããªã±ãŒã·ã§ã³ã¢ãŒããã¯ãã£ãã³ã³ãã/ã³ã³ããŒãã³ããã¿ãŒã³
â ããŒã27ïŒã³ãŒã¹ãããžã§ã¯ã