å€ãã®éçºè ã¯ãReactãšVueãæ¯èŒããããšã奜ã¿ãŸãã 誰ãããããã®ãã¬ãŒã ã¯ãŒã¯ã®1ã€ã«ç«ã¡æ¢ãŸããé åºã«åºå·ããŸãããã€ãŠã圌ããã€ãŠæåŠããå¥ã®ã©ã€ãã©ãªãç¥ãããšããããŸããã å€ãã®å Žåãæéã®åé¡ã§ããã·ã¹ãã ã®ãã¹ãŠã®å ¥åãšåºåãçã«ãã¹ã¿ãŒããã«ã¯ãã·ã¹ãã ãæäœããæŠããæé·ããå¿ èŠããããŸãã
ãã¡ãããåæ§ã®ããŒã«éã§ã®ã¹ãã¬ãŒã¯å¹ççã§ã¯ãããŸããããèå³ã¯ãããŸãããïŒ èå³ããããŸãã
ãto-doãªã¹ãããªã©ã®ãžã£ã³ã«ã§ã¢ããªã±ãŒã·ã§ã³ãäœæããæ¹æ³ã«ã€ããŠãã€ã³ã¿ãŒãããäžã«å€ãã®æ¯èŒèšäºããããŸãã VueãšReactã䜿çšããŸãããå®éã®ãããžã§ã¯ãã¯ããã»ã©åçŽã§ã¯ãããŸããã å®éã®ã¢ããªã±ãŒã·ã§ã³ã§ã¯ãã«ãŒãã£ã³ã°ãç¶æ ã®ç¶æããã©ã°ã€ã³ã®äºææ§ãªã©ã«æ³šæããå¿ èŠããããŸãã
Vueã©ã€ãã©ãªãšReactã©ã€ãã©ãªã®ããŒã¹ã«å«ãŸããŠããéãã§ã¯ãªãããããã®ãã¬ãŒã ã¯ãŒã¯ã䜿çšããŠå®éã®ã¢ããªã±ãŒã·ã§ã³ãäœæããæ©èœã«ã¯èå³ããããŸããã ããšãã°ãåäžããŒãžã®ã¢ããªã±ãŒã·ã§ã³ãéçºããå Žåãã©ã®ããŒã«ããã䟿å©ã§ããïŒ
çšé
ç§ã¯çŽ2幎éVueã䜿çšããŠãããçŽ8幎éWebéçºãè¡ã£ãŠããŸãã Vueã§åããŠèªåã®åãè©Šããã®ã§ã ã¡ã¢ãåãããã®ã·ã³ãã«ãªã¢ããªã±ãŒã·ã§ã³ããªãŒãã³ãœãŒã¹ã«å ¥ããããšã§ããªãŒãã³ã«ãå匷ããããšã決ããŸãããJWTã䜿çšããŠãŠãŒã¶ãŒãèªèšŒããããšãã§ããŸãã 圌ãžã®ã«ããã«ã§ãKoaã䜿çšããŠäœæããããã¯ãšã³ãã¢ããªã±ãŒã·ã§ã³ãäœæããŸããã
ç§ã¯ãã¬ãŒã ã¯ãŒã¯ãå€æŽããç·æ¥ã®å¿ èŠæ§ãæããŠããŸããã§ããããReactãåŠã¶ããšã¯çŽ æŽããããšèããŸããã ãããã£ãŠãç§ã¯Reactã§koa-vue-notesã¢ããªã±ãŒã·ã§ã³ãåäœæãããªãŒãã³ãœãŒã¹ã«ãæçš¿ããŸããã ãã®ãããªçµéšã¯ãå°ãªããšãJavaScriptã®ç解ãæ·±ãããã®ã§ãããèªåèªèº«ãæ°ãããæ°ã«å ¥ãã®ããŒã«ã§ãããšæããããããŸããã
ãããç§ã®ã¢ããªã±ãŒã·ã§ã³ã®ããŒã ããŒãžã§ãã äžãReactããŒãžã§ã³ãäžãVueã§ãïŒ
ç§ã¯ã¢ããªã±ãŒã·ã§ã³ã§BootstrapãããŸã䜿çšããŸããããéåžžã¯Bootstrap 4ã§å°å ¥ãããæ°ããNavbarã³ã³ããŒãã³ããã¢ããªã±ãŒã·ã§ã³ã«å®è£ ããŸããçµéšãšç 究ãç§ããªã¢ã¯ã·ã§ã³ã¹ãã©ããã«å°ããŸããã
ãã®å Žåãçµå±ã®ãšãããReactã§Bootstrapã°ãªããã䜿çšããªãã£ããã ã°ãªããã¹ã¿ã€ã«ã®ãªãã·ã§ã³ã«èœã¡çããããšã«æ³šæããå¿ èŠããããŸãã
ã¢ããªã±ãŒã·ã§ã³ã§ã¯ããŠãŒã¶ãŒãšã®ãµã€ã³ã¢ãã/ãã°ã€ã³/å¿ãã/ãªã»ããæäœãå®è¡ãã圌ã®ããŒãã§äœæ/èªã¿åã/ç·šé/åé€ããããšãã§ããŸãã ç»é²ãé¢åãªå Žåã¯ãdemousernameãšdemopasswordã§ãã°ã€ã³ããŸãã
ãã£ã¬ã¯ããªãšãœãŒã¹ã³ãŒãã®æ¯èŒ
第äžå°è±¡
Reactã䜿çšãããšã1ã€ã®ããšãããã«æããã«ãªããŸããJavaScriptã«éåžžã«å¯æ¥ã«å¯ŸåŠããå¿ èŠããããŸãã
ç§ã¯ããããªã¹ãã®åŸåãé å®ããå¿ èŠã®ãªããŽãã¯ãã¹ãŠåãé€ããŸãã ãããã£ãŠãReactãå®äŸ¡ã§æãã®æ§è³ªã§ç§ãã©ã®ããã«æ¹ãã€ãããã¯ç°¡åã«ããããŸãã
React-RouterãšVue-Routerãæ¯èŒãã
React-Routerã¯ãReactã®ã¢ã¯ãã£ãã«ãŒãã£ã³ã°ã·ã¹ãã ã§ãã 圌女ã¯åªããã¹ããŒããæã£ãŠããŸãããå®éã«ã¯åœŒå¥³ã«å¯ŸåŠããããã€ãã®èå³æ·±ãåé¡ã«åºäŒããŸããã åºæ¬çãªèšå®ã¯éåžžã«ç°¡åã§ãããReact-Router v4ã§å¿ èŠãªããã«ãHTMLã§çŽæ¥ã«ãŒãã宣èšããã®ã¯å¥œãã§ã¯ãããŸããïŒä»¥åã®ããŒãžã§ã³ã®React-Routerã§ã¯ç¶æ³ãç°ãªããŸããïŒã
ã«ãŒãã®åæãç¶ãããšã次ã®åé¡ãçºçããŸããããŠãŒã¶ãŒãã¢ã¯ã»ã¹ããŠã¯ãããªãããŒãžã«ãŠãŒã¶ãŒãã¢ã¯ã»ã¹ã§ããªãããã«ããæ¹æ³ã§ãã åºæ¬çãªäŸïŒãŠãŒã¶ãŒã¯ããã°ã€ã³ããã«ã¢ã«ãŠã³ãã¿ã€ãã®ããŒãžãéãããšããŸãã React-Routerã䜿çšããŠæçµçãªãœãªã¥ãŒã·ã§ã³ãçºè¡ããããã«ãè©Šè¡é¯èª€ã«ãã£ãŠç¶æ³ãšã¢ã¯ã·ã§ã³ã調æ»ããã®ã«æ°æéããããŸããã
æçµçã«ããã®ãããªåçŽãªæ©èœãå®è£ ããããã®ã³ãŒããã©ãã»ã©æ··ä¹±ããäžäŸ¿ã§ãããã«ã€ããŠã¯æºè¶³ããŠããŸããã§ããã 以äžã¯ããŠãŒã¶ãŒãç¹å®ã®ããŒãžã«ã¢ã¯ã»ã¹ããã®ããããã¯ããã³ãŒãã§ãã
Vue-Routerã¯ãã«ãŒãã£ã³ã°çšã®Vueç¬èªã®ã©ã€ãã©ãªã§ãã ã«ãŒã宣èšãã¡ã€ã«ã§çŽæ¥ã«ãŒãå®çŸ©ã«è¿œå æ å ±ãè¿œå ã§ããæ¹æ³ããšãŠãæ°ã«å ¥ã£ãŠããŸãã ãŠãŒã¶ãŒãã«ãŒãå®çŸ©ã®requireAuthããããã£ã䜿çšããŠVue-Routerã®ããŒãžã«ã¢ã¯ã»ã¹ããããšãçŠæ¢ããrouter.beforeEaché¢æ°ãæ€èšŒããæ¹æ³ã確èªããŸãã
ããŠãVueã³ãŒããèŠããšå°ãããªã¥ãŒã ããããŸãããããã¯ããã¥ã¡ã³ãã§èª¬æãããŠãããšãããªã®ã§ãã¢ããªã±ãŒã·ã§ã³ãæ§æããã®ã¯é£ãããããŸããã§ããã Reactã³ãŒãã«ã€ããŠãåãããšãèšããŸããã ããã§ãåã解決çãæãèµ·ããããããã«ãæ°æéããããŸããã ã¢ããªã±ãŒã·ã§ã³ãããŠãŒã¶ãŒã衚瀺ããã¯ãã®ãªãããŒãžã«ã¢ã¯ã»ã¹ããã®ãé²ããªã©ã®éèŠãªæ©èœãããã°ã©ã ããå¿ èŠãããå Žå...ãã®ãããªäœæ¥ã¯äžæ©ãããã¹ãã§ã¯ãããŸããã
ããã«ãURLã§ç·šéããŒãžããããŒã¿ãåéããããšãããšãReact-Routerã®ææ°ããŒãžã§ã³ã§ã¯ãã®æ©èœãåé€ãããŠããããšãããããŸããã ããã¯...ç§ã倱æãããã ãããè¡ãããçç±ã¯ç解ã§ãããšæããŸãïŒã¯ãšãªè¡ã®ããŒã¿ã¯ããããçš®é¡ã®ãã©ãŒã ãšãã©ãŒã ã§æäŸãããŸãããURLã§ãã©ã¡ãŒã¿ãååŸããããšãäžå¯èœã§ãã£ãŠããã©ããããããå€ãããŸãã URLãæ£ãã解æããããã«qsã©ã€ãã©ãªãããŠã³ããŒãããå¿ èŠããããŸããããæé äžã®åé¡ãèŠã€ãããŸããã 詳现ãªè°è«ã¯ãã¡ãã§ãã
ç§ã¯ãããç解ããããã«ããã¹ãŠã«ã€ããŠã®ãã¹ãŠã«äœåãªæéãè²»ãããŸããã ãã ããæãæ·±å»ãªåé¡ã§ã¯ãããŸããããVue-Routerã§çµéšããããšãšã¯å€§ããç°ãªããŸããã€ãŸããããã¥ã¡ã³ããèŠãŠããœãªã¥ãŒã·ã§ã³ãã³ãŒãã§å®è£ ããŸãã Vueã§ãããèšãããšããŠããã®ã§ã¯ãããŸããã人çã§ã¯ãªããããšã話ã§ãã Reactã®å Žåããã¹ãäºæ³ãããèããåä»ã§ããããšãå€æãããã®ãããªå°è±¡ãåããŸããã
ReduxãšVuexã®æ¯èŒ
Reduxã¯ãReactã§æã人æ°ã®ããããŒã¿ãŠã§ã¢ããŠã¹ã§ãããFluxãã³ãã¬ãŒãã«åºã¥ããŠããŸãã Fluxãããããªãå Žåã¯ã説æããŸããããã¯äžè¬ã«ãã¢ããªã±ãŒã·ã§ã³å ããã¢ã¯ã·ã§ã³ããã£ã¹ãããããããšã«ãã£ãŠç·šæãããåæ¹åããŒã¿ã¹ããªãŒã ã«åºã¥ãèšèšãã¿ãŒã³ã§ãã èšãæãããšãããããçš®é¡ã®ã³ã³ããŒãã³ãã®ããŒã¿ã«ã¢ã¯ã»ã¹ããŠæäœããããšãããšããã¹ãŠãæŽç¶ãšä¿æãããŸãã
ã¢ã¯ã·ã§ã³ãšãªãã¥ãŒãµãŒã䜿çšããŠã¬ã³ãŒããäœæããReduxãªããžããªã®ãã¡ã€ã«ã®äŸã次ã«ç€ºããŸãã
åºæ¬çã«ã¯ãã¢ã€ãã¢ã¯æ¬¡ã®ãšããã§ããã¹ãã¬ãŒãžããããŒã¿ãå®å šã«æäœããã¬ãã¥ãŒãµãŒãããªã¬ãŒããã¢ã¯ã·ã§ã³ããã£ã¹ãããããŸãã ãã®ããã«ããŠãåã³ã³ããŒãã³ãã¯å®å šã«ããŒã¿ãèªã¿åãããã®å€æŽã«å¿çã§ããŸãã
Vueäžçã®Vuexã¯Reduxãšåçã§ãã äž¡æ¹ã®ã©ã€ãã©ãªã¯ããã®åéã§éåžžã«åªãã瀟å ãµããŒããæäŸããŠããŸãã ã¬ãã¥ãŒãµãŒã®ä»£ããã«ãVuexã¯çªç¶å€ç°ã䜿çšããŠã¹ãã¬ãŒãžããŒã¿ãå®å šã«æŽæ°ããŸãã åœåã®ããããªéãã«å ããŠãäž¡æ¹ã®ã©ã€ãã©ãªã¯éåžžã«äŒŒãŠããŸãã 以äžã§ã¯ãsrc / store / note.jsã®Vueã¢ããªã±ãŒã·ã§ã³ã«åãæ©èœãå®è£ ããŸããïŒåœç¶ãäž¡æ¹ã®äŸã¯ãããã«çç¥ãããŠããŸãïŒã
æ£çŽãªãšãããReduxã¯Reactã®äŸ¿å©ã§åŒ·åãªãªããžããªã©ã€ãã©ãªã§ãããFluxã®åçã«è§Šçºãããããã«æããŸããã äœåãªã¹ãã¬ãªã¿ã€ãã®ã³ãŒããåå ã§åé¡ãçºçããŠããŸãã åœç¶ã®ããšãªãããç§ã¯ãã¹ãŠãç解ããã®ã§ããã¹ãŠãã·ã³ãã«ã§æ確ã«èŠããŸãããçµéšãããåå¿è ãReactçšã®æ確ã§ç°¡æœãªã³ãŒããå®è£ ããããã«Reduxã䜿çšããããšã¯å°é£ã§ããããšã瀺åãããŸã
ããšãã°ãä»ã®ã¢ã¯ã·ã§ã³ããã¢ã¯ã·ã§ã³ããã£ã¹ãããããã«ã¯redux-thunkã©ã€ãã©ãªã調ã¹ãŠã€ã³ã¹ããŒã«ããå¿ èŠããããç§ã«ãšã£ãŠã¯äžæå¿«ãªã¿ãŒã³ã§ããã ãã¡ãããredux-thunkã®ä»£ããã«redux- sagaã䜿çšãããredux-observableã䜿çšããããèããã®ã«ããã«æ°æéãè²»ãããŸããã ããããç§ã®è³ã¯ããã¿ãææ ã¯ãµã³ã¯ãšããèšèã§èª¬æããããšãã§ããŸãã
ããããã®ãããžã§ã¯ãã®æšªæçããŒãã§ããã æ¯èŒã®ããã«ãVuexãšã®çµ±åãèŠããŠãããŠãã ãã-ããšãã°ããããã¯æ¬åœã«ç§ãšäžç·ã§ããïŒããèªåã§ãããåããŠèšå®ããããšãèããããšãèŠããŠããŸãã
ã¬ã³ããªã³ã°
Reactã®ãã¹ãŠã®è©³çŽ°ã®äžã§ãã¬ã³ããªã³ã°é¢æ°ã¯ç§ã«ãšã£ãŠæãå¥åŠãªããšã®ããã«æããŸããã Vueã§ã¯ãããŒã¿ãç¹°ãè¿ãåŠçããŠèŠçŽ ãåãåºããããç¶æ /ã¹ãã¬ãŒãžå€æ°ã«å¿ããŠããŒã¿ã衚瀺/é衚瀺ãããããã®ããšãŠãç°¡åã§ãã Reactã§ã¯ãã¬ã³ãã©ãŒã®å€éšã§ããŒãã®ã«ãŒããäœæããå¿ èŠãããã®ã¯ããªãäžèªç¶ã«æããŸããã
Vueã§äœãã衚瀺ãŸãã¯é衚瀺ã«ããå Žåã¯ã次ã®ããã«ããŸãã
ãã®ã³ãŒãã¯ãmyVariableã®çå®ã«äŸåããŸãã Reactã§ã¯ãã©ããããããè¡ãå¿ èŠããããŸãïŒ
ã³ãŒãã¯å°ãé·ããªããVueã§v-forã䜿çšããŠæŽçã§ããã«ãŒãã®çŸåšã®å¯èœæ§ããµããŒãããŠããŸããã ãããããã¡ãããç§ããããã®å°ããªåçŽãªå°ããªããšãããããšã«æ £ãããšãããããã¯ãšãŠãå¥åŠã«èŠããªããªããŸããã äžè¬çã«ãæ £ããããšãã§ããŸãããReactã§ãããè¡ãããŸãã ããããVueãç¹å®ã®ããŒãžã¬ã€ã¢ãŠãã®ããŒã¿ã«ç°¡åã«ã¢ã¯ã»ã¹ã§ããããšã«æ³šæããå¿ èŠããããŸãã Reactã¯æ¬åœã«å°ããªãã«ããŒé¢æ°ã®ããã§ãã
ã¹ã¿ã€ã«ä»ãã³ã³ããŒãã³ã
ãã®ãããžã§ã¯ãã§ç§ãäžçªå¥œããªããšãç¥ã£ãŠããŸããïŒ ã¹ã¿ã€ã«ä»ãã³ã³ããŒãã³ã ã ç§ã¯åœŒããæäŸããã«ãã»ã«åãæ¬åœã«å¥œãã§ãã ã¯ããVueã§ã¯ãã³ã³ããŒãã³ãã®ã»ã¯ã·ã§ã³ã§ã¹ã³ãŒãããããã£ãåºå®ããååãšããŠåãããšãè¡ãããšãã§ããŸãã
åã³ã³ããŒãã³ããå°ããªãããèªäœãã«å€ããæ¹æ³ã«ã€ããŠãæ¬åœã«ã¹ã ãŒãºã§æ¥œãããã®ããããŸããã ä»»æã®ããããã£ïŒå°éå ·ïŒã®è»¢éã«ã¯ããã€ãã®å°é£ããããŸããããããã®ããã€ãã®è©³çŽ°ã解決ããã®ã§ãäœæ¥ããã®ã楜ãããªããŸããã ãã®èããå®å šã«ç解ãããŠãŒã¶ãŒã®ã³ã¡ã³ãã1ã€èŠããŠããŸãããã³ã³ããŒãã³ããã©ã®ããã«èšèšããããäºåã«èããã®ã¯æ £ããŠããŸããã
ReactãŠãŒã¶ãŒãæ¬åœã«ç°¡åã«ç解ã§ããçç±ã¯ãã³ã³ããŒãã³ãã®èšèšã以åã¯å°ãåä»ã ã£ãããã ãšæããŸãã Vueã§å©çšå¯èœãªåäžãã¡ã€ã«ã³ã³ããŒãã³ãã®å šäžçã«å°ãçããããããšæããŸãã ãã®ãããžã§ã¯ãã§ã¯ãåäžãã¡ã€ã«ã®ã³ã³ããŒãã³ããããã«è©äŸ¡ããããšãã§ããŸãããããã¯æ¬åœã«èŽåœçãªæ©èœã§ãã
Create-React-AppãšVue-CLIã®æ¯èŒ
create-react-appãæ¬åœã«æ°ã«å ¥ããŸããã ç§ã¯vue-cliã®ãã¡ã³ã§ãããcreate-react-appãªãã·ã§ã³ã¯äŸ¡å€ã®ãã競äºçžæã§ãã 詳现ãç解ããããã«ããã¹ãŠã®ãŠãŒã¶ãŒãWebpackã®ã€ã³ã¹ã¿ã³ã¹ããŒãããã€ã³ã¹ããŒã«ããããšããå§ãããŸãã ããããçç£ã«å®å®ãããã®ãå¿ èŠãªå Žåã¯ãæ¢è£œã®è¶³å ŽããŒã«ã䜿çšããããšã匷ããå§ãããŸãã
éçºããŒã«
ãŸãã泚ïŒReduxãšReactã®éçºããŒã«ã¯ãVueããŒã«ã»ã©åªããŠããŸãããããã¯ããã¶ã€ã³ãšè²ã®äž¡æ¹ã«é©çšãããã³ã³ããŒãã³ãã®ç¶æ ã確èªããããã ãã«ãã³ã³ããŒãã³ãã®å·šå€§ãªããªãŒãéãå¿ èŠããããŸãã ãã®ã¢ãŒãã§ã¯ãã¢ããªã±ãŒã·ã§ã³å€æ°ãç£èŠããã®ã¯ããªãå°é£ã§ããã
äœãã足ããªãããçŸåšã³ãã¥ããã£ã§æå¹ãªæšæºãšèŠãªãããŠããªãããŒãžã§ã³ã䜿çšããŠããå¯èœæ§ããããŸãã Vueã®ããŒã«ã¯ãèªåŒµãããçŽ æŽããããã®ã§ãèŠæ ãããããããŸãæ©èœããè¯å¿çã«æ©èœããŠããããã«èŠããŸãã ãããã®ããŒã«ã䜿çšããŠã©ãã ãã®æéãè²»ããããèãããšããããã®å°ããªè©³çŽ°ãããã«éèŠã§ããããç解ã§ããŸãã
ãããã«
ä»ã®æ¡ä»¶ãåãã§ããã°ãReactã®åŠç¿ã«æéãè²»ãããããšãéåžžã«å¬ããæããŸãã ç§ã¯åœŒãšäžç·ã«ä»äºãããããšãšäžè¬çãªããã°ã©ãã³ã°ã®äž¡æ¹ã§ãŸã ã¯ãªãã«ã¯ã§ããããšãç¥ã£ãŠããŸãããå°ãªããšãä»ã§ã¯ãããã€ãã®è€éãªããšãç¿åŸããæŠå¿µã«ç²ŸéããŠããŸãã ãŸããä»åŸã¢ãã€ã«ã¢ããªã±ãŒã·ã§ã³ã®éçºãéå§ããå¿ èŠãããå Žåã«åããŠãReact Nativeãè©Šãäºå®ã§ãã ãã®ãããªçµéšã¯ééããªã害ã¯ãããŸããã
ããããªããšã®æ¯èŒã«æãŠããªãè¡ãããšãã§ããŸããã å®éããã®èšäºã¯Vue / Reactã®æ¯èŒã«ã€ããŠèšããããšã®ã»ãã®äžéšã§ãã ã¢ããªã±ãŒã·ã§ã³ãè©ŠããŠãã ãã-ç§ã¯ããã«ã€ããŠç©æ¥µçã«ã³ã¡ã³ãããŠããŸãããããããã®ãã³ããšã³ãã圹ç«ã¡ãŸãã
çµè«ïŒç§ã¯Vueã§æ¬¡ã®ãããžã§ã¯ããè¡ã£ãŠããŸãã Reactã«å¯ŸåŠããããšã¯ã§ããŸãããè£ åãå°ã匱ããªã£ãŠããŸãã äžèŠãããã¯æ¥œããããšã§ããããŸãããäœãããããšããã«ãå®éã«å¿ èŠãªã³ãŒããããæããã«å€ãã®ã³ãŒããæžããŠããããšãããã«ããããŸãã
»äºçŽäºçŽãžã®ãªã³ã¯
çŽçã¯3ææ«ã«è¡šç€ºãããŸãã
ããŒã«ãŒã®ã¯ãŒãã³ã25ïŒ ãªã-Vue.js