å 責äºé
芪æãªãèªè ïŒ ReactãšReduxãäœã§ãããããããªãå Žåã¯ãããã«èªãããšã¯æå³ããããŸããã ãã®ããŒãã®ç®çãçå£ã«ç解ããã«ã¯ããããã®ã©ã€ãã©ãªãæäœããå¿ èŠããããŸã-æ確ã«èšè¿°ããããšããŸããããã®èšäºã¯ãšã³ããªãŒã¬ãã«ã®èšäºã§ã¯ãããŸããã ãããŠãããã¯å®è·µã«åºã¥ãå人çãªçµéšãšæèŠã§ãã
Reduxã®äœ¿çšã®äœãåé¡ã«ãªã£ãŠããŸããïŒ
ããããç§ã¯æžãããšã«ããŸããããå®éã«ç§ã®ãããžã§ã¯ããä»ã®äœåãã®reduxã䜿çšããããšã§äœãééã£ãŠããŸããïŒ ç§ã¯ã2016幎4æïŒ3幎ïŒä»¥æ¥ãreact / reduxã¢ããªã±ãŒã·ã§ã³ãæžããŠããŸãã èå³æ·±ããã®ããã§ã«çºèŠããæãæ¥ãŸãã...ãããŠãç¹ã«åå¿è åãã®è¬çŸ©ãã¬ããŒãããããŸãããã倧人ãæ¯ãè¿ãããšã¯ãªããå顧å±ããããŸããã§ããã ãããŸã§ã®éã誰ããã1æéã«13ãããªãããããã§ãã¯ããããã±ãŒãžã«ã¢ã¹ã¿ãªã¹ã¯ãå ¥ããŸããç§ã¯äžè¬çãªã¹ãã¬ãªã¿ã€ãã®å£ãç ŽããŸãã
ããããreduxã¯å¿ èŠãããŸããïŒ
ããªãã¯èªåãäœãã«æ£ãããšèšãã§ãããã 2018幎以éã¯äœ¿çšã§ããŸããããã®ãããã¯ã«é¢ããã€ã³ã¿ãŒãããäžã®èšäºã¯å±±ã»ã©ãããŸããããããŸã§ã®ãšãããäžäœ¿çšãã§éå£èŸ²å ŽãåŸãããŠããããã以äžã§çç±ãæããã«ãªããŸãã ãããŠãéžæè¢ã®æ°ã¯ã¹ã±ãŒã«å€ã§ããããã以äžã§ãã
Reduxã䜿çšããŠããã®ã¯ãïŒå°ãªããšãåå¿ã«é¢ããŠïŒåãå ¥ããããŠããæšæºã§ãããããReduxã®äºæž¬å¯èœæ§ãšä¿¡é Œæ§ã¯ç§ãã¡ã«ãšã£ãŠéèŠã§ãã ããããç§ãã¡ã¯çå£ã«ãããèŠéããŠããŸããå®éã«ã¯ããã§
ç³ãç«ãŠ
ããããããã€ã³ãã«ã€ããŠã®äž»åŒµãäœã§ããããæ確ã«ããããã«ãæåã«éå»ã«æ»ã£ãŠã奜ããªããã«ã«ãŒãã«æ»ãå¿ èŠããããŸããã€ãŸããæ å ã®ãªãã¥ãŒã¹ã®ããã¥ã¡ã³ããéããä»®å®ãèªã¿ãŸãã ç§ã¯ãããã®æåã®ãã®ã ããæ€èšããŸãã ãããããªãã«ãšã£ãŠèå³æ·±ãå Žå-ã³ã¡ã³ããå ±æããŠãç§ã¯ãã£ãšå€ãã®ãã€ã³ããåæããŸãã
å¯äžã®çå®ã®æº...
ããã«åŸ ã¡äŒãããããŸãã ãã¡ãããããã¯1ã€ã®ç©èªããããããããªããšèšããŸããreduxã¯ãã®ããã«ãã©ãã¯ã¹ã¢ãŒããã¯ãã£ãšã®éãã宣èšããŸãã ãããããã£ãšåºãèŠãŠã¿ããšãå®éã®ãããžã§ã¯ãã§ã«ãŒã«ãå®ãããŠããŸããïŒ ããªãã¯èšãã§ãããïŒããªãã ç§ã¯1ã€ã®åŽã宣èšïŒå®£èšïŒãããããã€ããŒã«è»¢éããŠãã...
æè¡çã«ã¯ãããã»ã¹ã¯æ£ããèšè¿°ãããŠããŸãã ããããç§ã¯äººã ã¯ç¥èŠãè«çã®æªã¿ã®åœ±é¿ãåãããããšèšãããšãã§ããŸãããããŠããã°ã©ããŒã¯ãŸã 人éãªã®ã§...ããªãã¯ç§ãå°ãããšãç解ããŠããŸãïŒããªããç解ããŠããªããã°ãããã°ã©ããŒã¯ç¥èŠãè«çãªã©ã®æªã¿ãèµ·ãããããã§ãïŒ
ããã§ã®äž»ãªæªã¿ã¯ãååã®å€ããããã§ããããã«ãredux以å€ã«é¢é£ããŠãã¹ãã¢ããšããçšèªã䜿çšããªãå Žåãä»ã«äœãååšããªããšããäºå®ã«æ £ããŠãããšããããšã§ãã
ãããŠãããã«åå¿ãæ¥ãŸã
å éšç¶æ ãšåŒã°ããæè¡çãªæ©èœã®1ã€ã¯ããã®ä»®å®ã«åºã¥ããŠããŸããã 䟿å©ãªå Žæã«å éšç¶æ ã®ãããªèšæ¶åãäœæããã ãã§ãã ã³ã³ããŒãã³ãããããŸã-ã³ã³ããŒãã³ããæŽæ°ãã圱é¿ãäžããã¡ã«ããºã ãããç¶æ ããããŸãã 䜿çšã®éãïŒã¹ãã¢ç¶æ ãæŽæ°ããããŒããã£ã¹ãã®å€æŽïŒã¯ã»ãšãã©ç®ã«èŠããŸããã ããªãã¯å察ããããšãã§ããŸãïŒã³ã³ããŒãã³ãã®ç¶æ ãæ°ã«å ¥ããªãçç±ã¯å®å šã«ã¯æããã§ã¯ãããŸããã 圌ã¯ç·šéè ã®ãããªãã®ã§ã¯ãããŸããã ããã¯ãå éšã«æ ŒçŽãããŠãããã©ã°ã§ãã
é ç®ã®ååãå€æŽããŠããæ¬è³ªã¯å€ãããªãããšãç解ããŠãã ããã æææ¥ã®å€§ãã³ããŒããããŸãããããã¯ææ¥ã«ã¯ãªããŸããã ã¯ããäž¡æ¹ã®æææ¥ã¯å³ããã§ããä»ã®æ¥ãšå€§ãã³ããŒã¯ç°¡åã§ãã ãããããã®ååããããã³ããŒã¯æ楜åšã§ã¯ãªããªããŸããã
ReduxãšState Reactã³ã³ããŒãã³ãã®èŠæš¡ã¯ç°ãªããŸãããä»æ¥äœ¿çšãããæ¬è³ªã¯1ã€ã§ãã
ãã®ããã«èª¬æããŸãïŒã»ãšãã©ã®å Žåãå éšç¶æ ã³ã³ããŒãã³ãããã®ããŒã¿ã¯å°éå ·ãä»ããŠåã«ãªããŸããããããã©ãã»ã©æçœã§ãã£ãŠãããªãã¥ãŒã¹ããŒã¿ã¯ãreactãšçµ±åãããå Žåãå°éå ·ãä»ããŠã³ã³ããŒãã³ãã«ãå ¥ããŸãã å°éå ·ãåãå ¥ããã³ã³ããŒãã³ãã®èŠ³ç¹ããã¯ãå€ã«äœããã£ãŠãããŸããŸããã ã€ãŸãããšã³ããŠãŒã¶ãŒïŒãã®å éšã¹ãã¢ïŒã¯åãã§ãã
ãŸãããã®å éšç¶æ ã¯ãããã宣èšãããŠããã³ã³ããŒãã³ãã®å°éå ·ã«äŸåããªãå ŽåããããŸãã 次ã«ããã®ãããªå éšç¶æ ãæ³åãããããã«å€§ããªã¹ãã¢ã«ããåé¢ãååŸããŸãã
ãããçã«å éšã§ããããã«ã¯ãåã³ã³ããŒãã³ãã«ãªãŒã¯ããããšãªãã宣èšãããŠããã³ã³ããŒãã³ãã«ã®ã¿åœ±é¿ãäžããå¿ èŠããããŸãã é£ããã§ããïŒ ãã®æå³ã¯ã»ãšãã©å®å šã«å€±ãããŠããããã§ãã ããã¯ãå éšç¶æ ãã¹ãã¢ã§ããããšã瀺ãå¥ã®å åã§ãã çµå±ã®ãšããããç¶æ ãä¿åããå€æŽãæŽæ°ããŠãããŒããã£ã¹ãããããšããç®çãã1ã€ã®ãã€ã³ããåé€ããã ãã§ããå€æŽããããŒããã£ã¹ãããŸãã ãã¹ãŠãç¶æ ã¯å€±ãããŸãã
ã€ãŸããå éšç¶æ ãæã€ããšã®äž»ãªåé¡ã¯ãããŒã¿ã®åé·æ§ãšç«¶åããé·æçã«ã¯å€±ããããã ããªãããšã§ãã ãªããã¢ããïŒããã¯ãã¹ãèŠçŽ ã®å åŒãå¿ èŠãªãšãã§ãããã®ãããã¹ããŒãéšåãšãããæäœãããã¹ãŠã®ããžãã¯ã芪ã«æž¡ãããäœæ¥ããžãã¯ã®æžãæããšãã¹ãã«å€ãã®æéãè²»ãããŸãïŒãªã©ã®ããããçš®é¡ã®ãã¯ããã¯ããããŸãããã°ã衚瀺ãããŸãããªãŒããŒãããæ¹åãšå€ãã®åã³ã§ã ããã¯ãéçºæ®µéã§ãœãããŠã§ã¢ãæãªããã¹ãŠã®ãã€ãºã§ãã ãŸã 販売ã«è³ã£ãŠããŸãããããœãããŠã§ã¢ã¯ãã§ã«ãã®ãããªãã®ã§ãã
ã€ãŸãããã¹ãŠã®å åãšåé¡ã«ã€ããŠãè€æ°ã®ã¹ããŒãªãŒãšããã«é¢é£ããå€ãã®åé¡ããããŸãã æåŸã®ããšã¯ãããããã次ã®ãšããã§ãã
ç§ã¯reduxãæã£ãŠããçš®é¡ã®devtoolsã«ã€ããŠãæ¬åœã«å€§å¥œãã§ãã ç§ãå§ãããšããç§ãã¡ã¯ãã¬ãŒã䜿çšããŸããããã¬ãŒã¯ãã¹ãŠã®ã¢ã¯ã·ã§ã³ãåçŽã«çµ±åããŸããããäœãèµ·ãã£ãŠããã®ããå®å šã«ã¯ææ¡ããŠããŸããã§ããã ããŠããããäž»ãªã¢ã·ã¹ã¿ã³ãã§ããå人ã§ãã Reactã§ã¯ãããããè¡šãããŸãã äžè¬ã«ãpubsubãReduxããé ãã®ã¯ãdevtoolsãçç±ã§ãã ã·ããã¬ã¹ã¯ãžã©ã®ã¢ãªã®ããã«ã
åé¡ ïŒDNAã®èšŒæ ã¯ãããŸããïŒïŒ
- React Devtoolsã䜿çšããŠå éšç¶æ ãå€æŽããŠããæŽæ°ãç®çã®çµæãåŸãããªãå ŽåããããŸãã
- å éšç¶æ ã¯redux devtoolsã®timetravelãå£ããŸãã Reduxã¢ãŒããã¯ãã£ãä»ããŠå©çšã§ããã¿ã€ã ãã©ãã«ã®ã¹ãŒããŒæ©èœã¯ãReactå éšç¶æ ã¢ãŒããã¯ãã£ã®ãããã§æ©èœããŸããã å éšç¶æ ã¯reduxã®å€æŽãæ°ã«ãããç¬èªã®ç¶æ ãæã£ãŠããŸãã ã¿ã€ã ãã©ãã«ã¯ããŸããããŸããã äžéšã®èŠçŽ ã¯ãåã«æŽæ°ãããªããéšåçã«æŽæ°ãããããªã©ã§ãããã®å£®å€§ãªãã®ã¯ãã¹ãŠãéåæã³ãŒãåæã䌎ããã®ã§ãã
ãã¡ãããç·Žç¿ããåžã蟌ãŸããäŸ
æ°ãããããžã§ã¯ãã«åãçµãã§ããããååã1幎åã«ããã€ãã®æ©èœãäœæããŠããŠããããæ¡åŒµããå¿ èŠããããŸãã äžè¬ã«ãä»ã®äººã®ã³ãŒããå®æãããã¿ã¹ã¯ããããŸãã 調æ»ãéå§ãããšãã£ã¿ãŒã«ããŒã¿ããªãããšãç解ããŸãã å¿ èŠãªããŒã¿ãä¿åããã³ãŒãã«ã¯ã¢ã¯ã·ã§ã³ãã¬ãã¥ãŒãµãŒã¯ãããŸããã ãããŠãããªãã¯å€§åãªãã®ãæ¢ããŠã³ã³ããŒãã³ãã®ããªãŒãæ ãå§ããããããæ°åã§ãèŠã€ããŸãïŒ!!!ïŒã ååã«å°ããŸãããçãã¯æšæºçãªãã®ã§ããå·ãžã®æžã蟌ã¿ãæ©ãèŠããŠããªãã®ã§ãæéããããŸããã§ããã ããªãã¯ãœãŒã¹ã«è¡ãããã®çŸåšã®ç¶æ ãæ¹èšã䌎ããªãããšãç解ããŸãã åäœãããã¹ãæžã¿ã®ã³ãŒããæžãæããŠãå€æŽãå ããæ°ããæ©èœãè¿œå ããŠããŸãã
å éšç¶æ ãšãã圢ã§ã®æ害ãªä»£æ¿ç©ã®ååšã¯ããã®æ±ãè¡çºãããŸãã çµå±ã®ãšãããä»ã§ã¯å®äŸ¡ã§ããã1幎ã§äœãèµ·ãããã¯é¢ä¿ãããŸããã
æ¯metaãå°ãªã
ããã¯æªãé£ã¹ç©ã®ããã«èŠããŸã-ããã¯çŸå³ãããŠå®ãããã«èŠããŸããã1幎ã3幎åŸã«ã¯-æ¶å管ã¯åŸãã®ããããèªåã®äººçãçããŸãã ããªãã¯ä»¥åã®å¥åº·ãåãæ»ãããã«å€ãã®æéãšãéãè²»ãããåžžã«ããã§æåãããšã¯éããŸããã
ReduxãšReact Internal State㯠ã1ã€ã®ãããã«ããã倧äŒæ¥ãšäžå°äŒæ¥ã®ããã«ç«¶åä»ç€Ÿã§ãã äž»ãªè£œåã¯ããŒã¿ãšåœ±é¿åã§ãã ãœãããŠã§ã¢ã¯è£œåã®æ¶è²»è ã§ãã å€ãã®é¡æšããããŸãããæ¬è³ªã¯åããŸãŸã§ã-ãœãããŠã§ã¢ãéçºãããšãã競äºã¯å¿ èŠãããŸããã
ç§ãã¡ã¯ããã°ã©ã ã³ãŒãã®ãç¬è£è ãã§ããã競äºã¯æå¶ãããèªç±åžå Žã¯çŠæ¢ãããèšç»ãããçµæžãšåœå®¶ã®ç¬å ãæ¶è²»è ãæ¯é ãã¹ãã§ãã
ãšãã ãäœããç§ãéå±ãããã äžè¬ã«ããã¹ãŠãèšç»ã©ããã«é²ãå¿ èŠããããŸãã ã¹ããªã³ãããªãªãŒã¹ãªã©ãããããœãããŠã§ã¢ã«ã¯æéã®ã³ã¹ããšåžå Žãžã®å¯¿åœ/åå ¥ããããŸãã ããã¯éåžžã«éèŠã§ãããè¹ã®æŽåãå³æžé€šã®èèµ·ãèš±å¯ããããšã¯ã§ããŸããã
çµè«ã¯ç°¡åã§ãã
reduxã§ä»ã®ãªããžããªã䜿çšããªãã§ãã ããã äŸå€ã¯ãéåžžã«å€ç«ããã±ãŒã¹ã«ããã§ããŸããã ããšãã°ãååãšããŠã察å¿ããã¬ã€ã€ãŒãªãã§ã¯reduxã«ãã£ãŠå¶åŸ¡ãããã圱é¿ãäžããªãã³ã³ããŒãã³ãã
äŸ
ãããã©ã³ãã§ã¹ã¿ã³ãã¢ãã³ã¢ãžã¥ãŒã«ãéçºããå¥ã®ãã©ã³ãã§ã¹ãã¢ããªãã¡ã¯ã¿ãªã³ã°ããŸãããäžè¬ã«ãã¹ãã¢ãšç¶æ ã管çããããã®ã¢ãããŒãã¯ãå ¬éçšã®å¥ã®ãããã¯ã§ãã ã¢ãžã¥ãŒã«ã®ãªãã¡ã¯ã¿ãªã³ã°ãéå§ããŸããããã¢ãžã¥ãŒã«ã®äœæã®éå§æãšçµäºæã®äž¡æ¹ã§ããã¹ããšãŠã£ã¶ãŒããžã®ãªãã¡ã¯ã¿ãªã³ã°ã¯ãªããªããŸããã§ããã ãªãã¡ã¯ã¿ãªã³ã°ã¯å€§èŠæš¡ã§ãããèšç»ãå¿ èŠãªææ ®æ·±ããªã³ãŒã¹ãå¿ èŠã§ã-äžè¬çã«ãããªãã¯ãã ãªãã¡ã¯ã¿ãªã³ã°ããŠãªãã¡ã¯ã¿ãªã³ã°ããããšã¯ã§ããŸããã
ãããã£ãŠãã¹ãã¢ã§ä»åŸè¡ãããå€æŽã«ã€ããŠç¥ã£ãŠããã®ã§ãæ°ããæ©èœãéçºããããã«ããã䜿çšããŸããã§ããã ããã«ãããæŸæ£ããããªãã¡ã¯ã¿ãªã³ã°ãšãã¹ããæŽæ°ããã³ã¹ããå¢å ããå ŽåããããŸãã
ç§ãããããšïŒæå°éã®ããŒã¿ã«ãµã€ã³ã¢ããããŸããã ããŒã¿ãšãã®æ§é ã¯ãªãã¡ã¯ã¿ãªã³ã°ã®åœ±é¿ãåãããããããçæããã³ãŒãã¯åœ±é¿ãåãããä¿åããããããŸããããšãã£ã¿ãŒã«ãã€ããæžããŸããã§ããã ãŠãŒã¶ãŒããã°ã€ã³ããŠãããã©ãããããã«ããã€ãã®ãã£ãŒã«ãã確èªããŸãã
ããŒãºã«åãããŠãPubSubããã£ãã«ãšã·ã³ãã«ãªAPIã§æŽæµããŸããã ã¯ããã¯ããpubsabã éåžžã®çã¿ã䌎ãçã¿ã®æ¬ åŠã ã¿ã€ã ãã©ãã«-çã¿ã äžè¬çã«ãéçºããŒã«ã®åœ¢ã§chromeã®æ¡åŒµæ©èœãäœæããäºå®ã§ãããgithubã§reduxã®ç«¶åãšããŠåå®è£ ãå ¬éããããšãå¯èœã§ãã reduxã«ã€ããŠã¯ããã®èšäºã§ã¯åãäžããŸããããPubSubã«ã¯å®éã«ã¯ãããŸããã reduxãã¬ãŒãæãåºãããšããäºå®ã«å ããŠ...
ãããã£ãŠãã¢ãžã¥ãŒã«ã«ã¯ç¬èªã®ã¹ãã¬ãŒãžããµãŒããŒãžã®ç¬èªã®æ¥ç¶ããããŸãã
ã€ãŸããreduxã¯ã¢ãžã¥ãŒã«ã«ãŸã£ãã圱é¿ãããå®éã«ã¯ãã®ã¹ãã¬ãŒãžã«åœ±é¿ãäžããããšã¯ã§ããŸããïŒãµãã¹ã¯ãªãã·ã§ã³ã«ã¯3ã€ã®ãã£ãŒã«ããããããŸããïŒããã¢ãžã¥ãŒã«ãšPubSubã®äž¡æ¹ã¯reduxã«åœ±é¿ããŸããã ãã®åé¢ã¯ç«¶äºãæé€ããŸãã
ãããŒã¿ãã©ãã«ä¿åããã®ãïŒããšãã質åã¯ãç§ãä»ãŸã§çµéšããããšã®ãªãã¢ãžã¥ãŒã«ãéçºãããšãã§ãã ããããå éšç¶æ ã«å¯Ÿãããªãã¥ãŒã¹ã«é¢ããŠã¯ãå€ãã®å Žåããã®è³ªåã¯ã»ãšãã©åžžã«çºçããŸãã ç§ã¯ãã®è³ªåã«ãã£ã±ããšçããããšã«ããŸããã
ç§ã®å»ºç¯çæèŠã¯ïŒ
ããŒã¿ãreduxïŒall-allã "internal"ã§ãïŒã«ã®ã¿ä¿åããŸããåå¿ãããžã§ã¯ãã«ã¡ã€ã³ãªããžããªãšããŠæ¥ç¶ãããŠããå Žåã競åãããªããžããªã䜿çšããªãã§ãã ããã ããã«ããããã®ã©ã€ãã©ãªãšãã®devtoolsã®ä¿¡é Œæ§ãšåœ±é¿ãå¢å ããŸãïŒã¿ã€ã ãã©ãã«ãšãã¹ãŠã®ããŒã¿ã段éçã«è¿œè·¡ããããšã§ãéçºãšæœåšçãªåé¡ã®æ€çŽ¢ãé«éåãããŸã-åæå€æŽã¯ããéããéåæã«ãªãããããªããŸãïŒã
ãããããå éšç¶æ ãéçºããå®å šã«é€å€ããã©ã€ãã©ãªãè¿œå ãã䟡å€ãããã§ããããïŒ ãŸãã¯ãå éšç¶æ ãreduxããã®éžæãªã©ã«çœ®ãæããŸããïŒ ç§ã¯ãã®ãããªãã®ã1幎åã«æžãå§ãã90ããŒã»ã³ããçµããããã«1ã€ã®ã¬ããŒããå®æœããŸããã äœãŠèšãã®ïŒ ããããå¿ èŠã§ããïŒ
ãã®ã¡ã¢ã楜ããã ããšãé¡ã£ãŠããŸã:)