ããã¯ãèšäºã Reduxã®çŽ æŽããããšãã ãïŒ ãžã£ã¹ãã£ã³ãã¡ã«ã³ãŒãèïŒã®ç¿»èš³ã§ãã
Reduxã¯ãReactã³ã³ããŒãã³ãã®ç¶æ ã䜿çšããŠäŒããã®ãé£ããè€éãªç¶æ ã®çžäºäœçšãå·§ã¿ã«åŠçããŸãã å®éãããã¯ãªããžã§ã¯ãæåããã°ã©ãã³ã°ã«ãèŠãããã¡ãã»ãŒãžã³ã°ã·ã¹ãã ã§ãããèšèªã«çŽæ¥çµã¿èŸŒãŸããŠããã®ã§ã¯ãªããã©ã€ãã©ãªãšããŠå®è£ ãããŠããŸãã OOPãšåæ§ã«ãReduxã¯å¶åŸ¡ãåŒã³åºãåŽããåä¿¡åŽã«è»¢éããŸããã€ã³ã¿ãŒãã§ãŒã¹ã¯ç¶æ ãçŽæ¥å¶åŸ¡ããŸããããåŠçã®ããã«ã¡ãã»ãŒãžãæž¡ããŸãã
ãã®ç¹ã§ãReduxã®ã¹ãã¬ãŒãžã¯ãªããžã§ã¯ãã§ãããã¬ãã¥ãŒãµãŒã¯ã¡ãœãããã³ãã©ãŒã§ãããã¢ã¯ã·ã§ã³ã¯ã¡ãã»ãŒãžã§ãã store.dispatch({ type:"foo", payload:"bar" })
ãstore.send(:foo, "bar")
store.dispatch({ type:"foo", payload:"bar" })
ãRubyã®store.send(:foo, "bar")
store.dispatch({ type:"foo", payload:"bar" })
åçã§ãã ããã«ãŠã§ã¢ã¯ã¢ã¹ãã¯ãæåããã°ã©ãã³ã°ïŒããšãã°Railsã®before_action
ïŒãšã»ãŒåãæ¹æ³ã§äœ¿çšããã before_action
ã®å©ããåããŠãreact-reduxã«äŸåæ§æ³šå
¥ãå®è£
ãããŸãã
ãã®ã¢ãããŒãã®å©ç¹ã¯äœã§ããïŒ
äžèšã®å¶åŸ¡ã®å転ã«ãããç¶æ å€æŽã®å®è£ ãå€æŽããããšããã«ããŠãŒã¶ãŒã€ã³ã¿ãŒãã§ã€ã¹ãæŽæ°ããå¿ èŠããªããªããŸãã ãã®ã³ã°ãåãæ¶ããã¿ã€ã ãã©ãã«ãããã°ãªã©ã®è€éãªæ©èœãè¿œå ããã®ã¯ç°¡åã§ãã çµ±åãã¹ãã¯ãæ£ããã¢ã¯ã·ã§ã³ãéä¿¡ããããã©ããã確èªããããã ãã«åæžãããæ®ãã«ã€ããŠã¯åäœãã¹ãã§ååã§ãã
Reactã®ã³ã³ããŒãã³ãã®ç¶æ ã¯ããŠãŒã¶ãŒæ å ±ãã¢ã©ãŒããªã©ãå€ãã®ã¢ããªã±ãŒã·ã§ã³ã¢ãžã¥ãŒã«ã«åœ±é¿ãäžãããšã³ãããŒãšã³ãã®æ©èœãšé£æºããã«ã¯é ãããŸãã Reduxã«ã¯ããã®ããã ãã«ããŠãŒã¶ãŒã€ã³ã¿ãŒãã§ã€ã¹ã«äŸåããªãç¶æ ããªãŒããããŸãã ããã«ãã€ã³ã¿ãŒãã§ã€ã¹ã®å€éšã§ç¶æ ãåŠçããå ŽåãlocalStorageãŸãã¯URLã§ã®ã·ãªã¢ã«åã1ãæã§å®è¡ããããããäžè²«æ§ãç¶æãããããªããŸãã
-
method_missing
ã¯ãçµåããããåæã«éä¿¡ããããmethod_missing
ã®ã¹ã¿ã€ã«ã§åŠçãããããããšã®ã§ããã¢ã¯ã·ã§ã³ãèªç±ã«æäœã§ããŸãã
ãããããããã®ã±ãŒã¹ã¯ãã¹ãŠç¹å¥ã§ãã ããåçŽãªã·ããªãªã¯ã©ãã§ããïŒ
ããã§åé¡ãçºçããŸãã
ã¢ã¯ã·ã§ã³ã¯ç¶æ éã®è€éãªé·ç§»ãšèŠãªãããšãã§ããŸãããåºæ¬çã«ã¯1ã€ã®å€ã®ã¿ãæå®ããŸãã Reduxã§äœæãããã¢ããªã±ãŒã·ã§ã³ã§ã¯ããããã®åçŽãªã¢ã¯ã·ã§ã³ã®å€ããé »ç¹ã«èç©ãããŸããããã¯ãã»ãã¿ãŒé¢æ°ãæåã§äœæããJavaãclearlyãšãããŸãã
ã¢ããªã±ãŒã·ã§ã³å šäœã§åãç¶æ ã䜿çšã§ããŸãããã»ãšãã©ã®å Žåãã€ã³ã¿ãŒãã§ãŒã¹ã®ç¹å®ã®éšåãåç §ããŸãã ã³ã³ããŒãã³ãããReduxãªããžããªãžã®ç¶æ ã®è»¢éã¯ãé©åãªã¬ãã«ã®æœè±¡åãªãã®åãªããªãã€ã¬ã¯ãã§ãã
- ã¬ãã¥ãŒãµãŒé¢æ°ã¯ãæãè€éãªã¡ã¿ããã°ã©ãã³ã°ã¿ã¹ã¯ã«å¯ŸåŠã§ããŸãããéåžžã¯ãã®ã¿ã€ãã«å¿ããããªããã£ããªãã£ã¹ãããã¢ã¯ã·ã§ã³ã«ãªããŸãã ããã¯ããã¿ãŒã³ãããã³ã°ã®ç°¡æœã§è¡šçŸåã®ããæ§æãç°ãªãElmãErlangã®ãããªèšèªã§ã¯åé¡ã«ãªããŸããããJavascriptã§ã¯é¢åãªã¹ã€ããã³ã³ã¹ãã©ã¯ããåŠçããå¿ èŠããããŸãã
ãã ããäž»ãªåé¡ã¯ãåçŽãªã±ãŒã¹ã®å®åæã®ãã®ãããªé·ãäœæ¥ã®åŸãäžè¬çã«è€éãªã±ãŒã¹ã§ã¯ããè¯ã解決çãããããšãå¿ããããšã§ãã æçµçã«ç¶æ ã®å·§åŠãªç§»è¡ã«çŽé¢ããŠãããªãã¯åã«æ°ããå€ãèšå®ããå€ãã®ç°ãªãã¢ã¯ã·ã§ã³ãéããŸãã ããã¬ãã¥ãŒãµãŒããå¥ã®ã¬ãã¥ãŒãµãŒã«ã¹ã€ãããªãã·ã§ã³ãã³ããŒã¢ã³ãããŒã¹ãããã©ãããã§ã䜿çšã§ããé¢æ°ã®åœ¢åŒã§æœè±¡åããŸããã
ããããã¹ãŠã人çèŠå ã«åž°ããã®ã¯ç°¡åã§ããããã¥ã¡ã³ããŒã·ã§ã³ããã¹ã¿ãŒããªãã£ãããŸãã¯ããã¹ã¿ãŒã¯æãã§ã-ãã€ãã¯æãã§ãã -ãããããã®ãããªåé¡ã¯ãã°ãã°çãããèŠã€ãããŸãã ãããã»ãšãã©ã®ãã¹ã¿ãŒã®ããã«æãã§ãããªãã°ããã€ãã«åé¡ããããŸããïŒ
ã ãããéåžžã®ã±ãŒã¹ã§ã¯Reduxããé¢ããŠãç¹å¥ãªã±ãŒã¹ã®ããã«ãããä¿åããæ¹ãè¯ãã§ããããïŒ
ReduxããŒã ãããã®ãããªã¢ããã€ã¹ãäžããããŸããååã«ãåãããšãèšããŸãsetState
ãå®å
šã«å¶åŸ¡ã§ããªããªããŸã§ããããåãäžããªãã§ãã ããã ããããReduxã䜿çšããçç±ã¯ãã€ã§ãèŠã€ããã®ã§ãç§èªèº«ãèªåã®ã«ãŒã«ã«ã¯åŸããŸããã set_$foo
ãããªå€ãã®ã¢ã¯ã·ã§ã³ããããå€ãå²ãåœãŠããããã³ã«ãURLãæŽæ°ãããããä»ã®äžéå€ããªã»ãããããŸãã ç¶æ
ãšãŠãŒã¶ãŒã€ã³ã¿ãŒãã§ã€ã¹ã®éã«æ確ã§æ確ãªå¯Ÿå¿ããããããããŸãããããã°ã€ã³ããããã¢ã¯ã·ã§ã³ããã£ã³ã»ã«ããå¿
èŠããããŸãã
å®éãç§ã¯Reduxã§æ£ããæžãæ¹æ³ãç¹ã«ãããæããæ¹æ³ãç¥ããŸããã ç§ãåãçµãã åã¢ããªã±ãŒã·ã§ã³ã«ã¯ããã®ãããªReduxã®ã¢ã³ããã¿ãŒã³ããããããããŸããããè¯ã解決çãèŠã€ãããªãããååã«äœããå€ããããã«èª¬åŸã§ããŸããã§ããã Reduxã®å°é家ãå¹³å¡ãªã³ãŒããæžããŠãããšèšãã°ãæ°äººã«ã€ããŠã¯äœãšèšããŸããã Reduxãäžåã«äžŠã¹ãŠäœ¿çšãããšããäžè¬çãªã¢ãããŒãã®ãã©ã³ã¹ããšãããšããŠããã ãã§ãã
ãã®å Žåã®å¯ŸåŠæ¹æ³
幞ããªããšã«ãReduxã¯ãJumpstartïŒ https://github.com/jumpsuit/jumpstate ïŒãªã©ã®åçŽãªãã®ãæäœããããã®ãµãŒãããŒãã£ã©ã€ãã©ãªãå«ããã®ã«ååãªæè»æ§ãåããŠããŸã ã 説æãããŠãã ããïŒReduxã¯äœã¬ãã«ã®ã¿ã¹ã¯ã«äœ¿çšã§ããªããšã¯æããŸããã ãããã®äœæ¥ããµãŒãããŒãã£ã®ã©ã€ãã©ãªã«äžããããå Žåãããã¯ç解ãè€éã«ããèªæã®æ³åã«ããã°ãäºçŽ°ãªããšã«æéãç¡é§ã«ãªããŸããåãŠãŒã¶ãŒã¯æçµçã«ã¯ç¬èªã®ãã¬ãŒã ã¯ãŒã¯ãéšåçã«æ§ç¯ããå¿ èŠãããããã§ãã
奜ããªäººãããŸãã
ãããŠç§ã¯åœŒãã®äžã«ããŸãïŒ ããããããã¯ãã¹ãŠã®äººã«åœãŠã¯ãŸãããã§ã¯ãããŸããã ç§ã¯Reduxã®å€§ãã¡ã³ã§ãããã»ãŒãã¹ãŠã®ãããžã§ã¯ãã§äœ¿çšããŠããŸãããæ°ããwebpackæ§æãè©ŠããŠã¿ããã§ãã ç§ã¯å žåçãªãŠãŒã¶ãŒã®äŸã§ã¯ãããŸããã Reduxã«åºã¥ããŠç¬èªã®æœè±¡åãäœæãããšãæ°ããæ©äŒãåŸãããŸãããå幎åã«ããã¥ã¡ã³ããæ®ããã«çµäºããã·ãã¢ãšã³ãžãã¢ãæžããæœè±¡åã¯äœããããããŸããïŒ
Reduxã解決ããè€éãªåé¡ã«åºãããå¯èœæ§ã¯ãããŸãããç¹ã«ãäžçŽã®ååããã®ãããªã¿ã¹ã¯ã«é¢äžããŠããããŒã ã®ãžã¥ãã¢ã§ããå Žåã¯ããã§ãã 次ã«ãReduxããã¹ãŠã3åæžãæãããããããªå¥åŠãªã©ã€ãã©ãªã§ãããšæ³åããŸãã Reduxã¯ãæ·±ãç解ããããšãªããæ©æ¢°çã«æ©èœããã®ã«ååãªã»ã©åçŽã§ãããããã«ã¯ã»ãšãã©åã³ãšå©çããããŸããã
ã ããç§ã¯ä»¥åã«å°ãã質åã«æ»ããŸãïŒã»ãšãã©ãããŒã«ã誀ã£ãŠäœ¿çšããå Žåãããã¯å šäœã®åé¡ã§ããïŒ é«å質ã®ããŒã«ã¯æçšã§èä¹ æ§ãããã ãã§ãªããããã䜿ã£ãŠäœæ¥ããã®ãå¿«é©ã§ãã æ£ãã䜿çšããã®ãæã䟿å©ã§ãã ãã®ãããªããŒã«ã¯ãä»äºã®ããã ãã§ãªãã人ã®ããã«ãäœãããŠããŸãã 楜åšã®å質ã¯ãããã䜿çšãããã¹ã¿ãŒã«é¢ããäœæè ã®æ³šæãåæ ããŠããŸãã
ãããŠãç§ãã¡ã¯ã©ã®ããã«ãã¹ã¿ãŒãæ°ã«ããŸããïŒ ããŒã«ã®å©äŸ¿æ§ã«åãçµãã®ã§ã¯ãªãããã¹ãŠãééã£ãŠãããšäž»åŒµããã®ã¯ãªãã§ããïŒ
é¢æ°åããã°ã©ãã³ã°ã«ãåæ§ã®çŸè±¡ããããŸããããã¯ãç§ããã¢ããã®æèšããšåŒã¶ãã®ã§ããã¢ãããã©ã®ããã«æ©èœãããã説æããã®ã¯ç°¡åã§ããããã®å©ç¹ãäŒããã®ã¯é©ãã»ã©å°é£ã§ãã
æçš¿ã®éäžã§ã¢ããã説æããããšã«çå£ã§ããïŒ
ã¢ããã¯Haskellã§äžè¬çãªæœè±¡åã§ããããªã¹ãã®æäœããšã©ãŒãç¶æ ãæéãI / Oã®åŠçãªã©ãããŸããŸãªèšç®ã«äœ¿çšãããŸãã do-notationã®åœ¢åŒã®æ§æç³ã¯ãåœä»€ã³ãŒãã«äŒŒã圢åŒã§ã¢ããã®æäœã®ã·ãŒã±ã³ã¹ãè¡šãããšãã§ããŸããããã¯ãéåæã³ãŒããåæã®ããã«èŠããJavaScriptã®ãžã§ãã¬ãŒã¿ãŒã«ã»ãŒäŒŒãŠããŸãã
æåã®åé¡ã¯ãã¢ããªã±ãŒã·ã§ã³ã®èŠ³ç¹ããã¢ãããèšè¿°ããããšã¯å®å šã«æ£ãããšã¯éããªãããšã§ãã ä¿®é士ã¯ãå¯äœçšãšé次èšç®ãæ±ãããã«Haskellã«ç»å ŽããŸããããæœè±¡çãªæå³ã§ã¯ããããã¯ãããšã¯äœã®é¢ä¿ããããŸããïŒåœŒãã¯åã«2ã€ã®é¢æ°ã®çžäºäœçšã«é¢ããäžé£ã®ã«ãŒã«ã§ãããç¹å¥ãªæå³ã¯ãããŸããã çµåæ§ã¯ãç®è¡æŒç®ãéåã®æŒç®ããªã¹ãã®çµåãããã³ãã«äŒæã«ãåæ§ã«é©çšã§ããŸãããããããšã¯ç¬ç«ããŠååšããŸãã
2çªç®ã®åé¡ã¯åé·æ§ã§ããããã¯ãå°ãªããšããåœä»€åã¢ãããŒããšæ¯èŒããã¢ããã®èŠèŠçãªè€éããæå³ããŸãã Maybe
ãããªæ確ã«å®çŸ©ããããªãã·ã§ã³åã¯ãèœãšãç©Žãnull
ãšããŠèŠã€ãããããå®å
šã§ããããããã®ã³ãŒãã¯é·ããŠæ±ãã«ããã§ãã Either
ã¿ã€ãã䜿çšãããšã©ãŒåŠçã¯ãäŸå€ãã©ãã«ã§ãã¹ããŒã§ããã³ãŒããããç解ããããããã«èŠããŸãããåæããããã«ãäŸå€ãå«ãã³ãŒãã¯ã Either
ã§å€ãå®æ°ã§è¿ããããç°¡æœã§ãã ç¶æ
ãå
¥å/åºåãªã©ã®å¯äœçšã«ã€ããŠã¯ãåœä»€åèšèªã§ã¯ãŸã£ããäºçŽ°ãªããšã§ãã é¢æ°åããã°ã©ãã³ã°ã®ãã¡ã³ïŒç§ãå«ãïŒã¯ãé¢æ°åèšèªã®å¯äœçšãæ±ãã®ã¯éåžžã«ç°¡åã ãšäž»åŒµããã§ãããããã©ããªçš®é¡ã®ããã°ã©ãã³ã°ãç°¡åã ãšçŽåŸã§ãã人ã¯ããªãã§ãããã
ãããã®äŸãåºãèŠãŠã¿ããšããã®å©ç¹ã¯æ¬åœã«é¡èã§ããã¢ããã®æ³åã«åŸãã ãã§ãªããåãæ³åã§ãã 1ã€ã®ã±ãŒã¹ã§æ©èœããäžé£ã®æäœããä»ã®ã±ãŒã¹ã§æ©èœããå ŽåããããŸãã ãªã¹ãã®ãã¢ããã¢ã®ãªã¹ãã«å€ããããšã¯ãæ¡ä»¶ã§Promise
ãªããžã§ã¯ãã®ãã¢ãçµã¿åãããŠãçµæãå«ãã¿ãã«ãè¿ããã®ãšåãã§ãã
ãªããããã¹ãŠãªã®ãïŒ
å®éãReduxã«ãåãåé¡ããããŸãã圌ãè€éã ããã§ã¯ãªããåçŽã ãããšãã£ãŠããã¬ãŒãã³ã°ããã®ã¯é£ããã§ãã ç解ã¯ãããç¥èã§ã¯ãªããåºæ¬åçãä¿¡é Œããããšã§ãããã®ãããã§ãåž°çŽã«ãã£ãŠäŒæ¯ããããšãã§ããŸãã
ãã®ç解ã¯å
±æããã®ãç°¡åã§ã¯ãããŸããããªããªããåºæ¬ååã¯å¹³å¡ãªå
¬çã«(prevState, action) => nextState
ããïŒãå¯äœçšãåé¿ãããïŒããæå³ãã»ãšãã©ãªããªãã»ã©æœè±¡çã§ããããã§ãïŒ (prevState, action) => nextState
ïŒã ç¹å®ã®äŸã¯åœ¹ã«ç«ããªãïŒReduxã®åé·æ§ã瀺ãã ãã§ããã®è¡šçŸåã¯ç€ºããªãã
æãã«éããç§ãã¡ã®å€ãã¯ãç§ãã¡ãã©ã®ããã«åœŒã«æ¥ãããããã«å¿ããŠããŸããŸãã ç§ãã¡ã®ãåçºãã¯ãç¹°ãè¿ãããééãã誀ãã®çµæã§ããããšããã¯ãèŠããŠããŸããã
ãããŠãããªãã¯äœãææ¡ããŸããïŒ
ç§ãã¡ã«åé¡ãããããšãèªèããŠã»ããã Reduxã¯ã·ã³ãã«ã§ãããç°¡åã§ã¯ãããŸããã ããã¯éçºè ã«ããåççãªæ±ºå®ã§ãããåæã«åŠ¥åã§ãã 䜿ãããããåªå ããŠã¡ã«ããºã ã®åçŽããéšåçã«ç ç²ã«ããããŒã«ã¯ãå€ãã®äººã ã«ãšã£ãŠæçšã§ãã ããããã³ãã¥ããã£ã¯ãããçš®ã®åŠ¥åãè¡ãããããšãèªèããŠããŸããã
ç§ã®æèŠã§ã¯ãReactãšReduxãæ¯èŒããã®ã¯èå³æ·±ãã§ããReactã¯Reduxãããã¯ããã«è€éã§ãAPIã¯ã¯ããã«åºããå¥åŠãªããšã«ãåŠç¿ãšäœ¿çšãç°¡åã§ãã React APIã§æ¬åœã«å¿
èŠãªã®ã¯ã React.createElement
ããã³ReactDOM.render
ã§ãããç¶æ
ãã³ã³ããŒãã³ãã®ã©ã€ããµã€ã¯ã«ãããã«ã¯DOMã€ãã³ããããç°ãªãæ¹æ³ã§åŠçã§ããŸãã ããããã¹ãŠã®æ©èœãReactã«å«ãããšãé£ãããªããŸããããæ¹åãããŸããã
ãååç¶æ
ãã¯æœè±¡çãªæŠå¿µã§ãããå®éã«å©çãããããã®ã¯ããããç解ããåŸã§ãã äžæ¹ãReactã§ã¯ãã©ã®ããã«åäœããã®ãç解ããŠããªããŠãã setState
ã³ã³ããŒãã³ãsetState
ãã¢ãããã¯ç¶æ
ãå¶åŸ¡ããŸãã ãã®ãããªè§£æ±ºçã¯çæ³çã§ã¯ãããŸãããäžè¬çã«ç¶æ
ãæŸæ£ããããå€æŽããšã«å¿
ãæŽæ°ããæ¹ãå¹ççã§ãã ãã®ã¡ãœããã¯ãéåæã«åŒã³åºããšäžå¿«ãªé©ããããããå¯èœæ§ããããŸãããããã§ãsetStateã¯çè«çãªæŠå¿µã§ã¯ãªããäœæ¥ã¡ãœãããšããŠã®Reactã«ãšã£ãŠã¯ããã«äŸ¿å©ã§ãã
ReduxéçºããŒã ãšãã®ãŠãŒã¶ãŒã³ãã¥ããã£ã¯ã©ã¡ããAPIæ¡åŒµã«ç©æ¥µçã«å察ããŠããŸãããçŸåšè¡ãããŠããããã«ãå€æ°ã®å°ããªã©ã€ãã©ãªããŸãšããããšã¯ãåå¿è ã«ãšã£ãŠãåä»ãªäœæ¥ã§ãããéå±ãªäœæ¥ã§ãã ReduxãåçŽãªã±ãŒã¹ã®çµã¿èŸŒã¿ãµããŒãã®ã¬ãã«ãŸã§æé·ã§ããªãå Žåããã®ããããå ãããæäžäž»ããã¬ãŒã ã¯ãŒã¯ãå¿ èŠã«ãªããŸãã ãžã£ã³ãã¹ãŒãã¯è¯ãåè£ã«ãªãå¯èœæ§ããããŸããå€å¯Ÿå€ã®é¢ä¿ã®æ§è³ªãç¶æããªãããç¹å®ã®æ©èœã®åœ¢ã§ã¢ã¯ã·ã§ã³ãšæ¡ä»¶ã®ã¢ã€ãã¢ãå ·äœåããŸãã ããããæãäž»ã®éžæã¯æãã®äºå®ã»ã©éèŠã§ã¯ãããŸããã
ç®èãªããšã«ãReduxã®æå³ã¯ãéçºè ãšã¯ã¹ããªãšã³ã¹ãã§ãããã³ã¯ãElmã®ãããªã¿ã€ã ãã©ãã«ãããã¬ãŒãåŠç¿ããã³åçããããReduxãèšèšããŸããã ããããã¢ã€ãã¢ãç¬èªã®åœ¢ãåããå®éã«Reactãšã³ã·ã¹ãã ã®ãªããžã§ã¯ãæåç°å¢ã«å€ãã£ãããããDXãã®å©äŸ¿æ§ãèæ¯ã«æ¶ããæ§æã®æè»æ§ã倱ãããŸããã ããã«ããReduxãšã³ã·ã¹ãã ãç¹æ ããŸããããã¢ã¯ãã£ããªãµããŒããåãã䟿å©ãªãã¬ãŒã ã¯ãŒã¯ãå¿ èŠãªå Žåã¯ã©ãã§ããã®ã£ããã¯ç©ºã§ãã Reduxã³ãã¥ããã£ã¯ãããæºããæºåãã§ããŠããŸããïŒ