æ¥ææ¥ã¨æææ¥ã使ã£ã¦React-Reduxã«å
¥éããã話ï¼
åå¼·ä¸ã®ã³ã¼ããªã®ã§ãã¾ãåèã«ãªããªãã¨æããï¼é±æ«ã®ææç©ã¨ãã¦GitHubãªãã¸ã㪠/daiz713/MyExpressAppã«ç½®ãã¨ããï¼ã¢ããªã®ãã¬ã¼ã ã¯ã¼ã¯ã«Node.jsã®Expressã使ã£ã¦ï¼Viewã«React+Reduxãæ¡ç¨ãã¦ããï¼ãã¼ã¿ãã¼ã¹ã¯ä»¥å使ã£ããã¨ã«ãã MongoDB ã«ãããã¨æã£ãããã© Google Cloud Datastore ã Google App Engine 以å¤ã®ã¦ã§ãã¢ããªãããå¼ã³åºãããã¨ãç¥ã£ãã®ã§ Datastore ã«ããï¼(Datastoreã¯ä»åã®ç¯å²ã§ã¯ã¾ã 使ããªã)
ãªãã¸ããªã®READMEã«æ¸ãã¦ãã /album
ã¨ããã®ãä¸çªæ°ããç·´ç¿ä½åã§ï¼æè¿Gyazoã£ãåçã表示ããããã¢ã«ãã ã¢ããª<Album />
ã¨ãªã£ã¦ããï¼Reactã³ã³ãã¼ãã³ã <Album />
å
ã«ã¯ï¼ããã¤ãã³ã³ãã¼ãã³ããå
å
ããã¦ãããï¼ã¡ã¤ã³ã¨ãªãã®ã¯ <GyazoImage />
㨠<GyazoImageList />
ã§ï¼ãããã«æ¸¡ã props ãæ´æ°ãã¦ãããã¨ã§ç»é¢ã®æç»å
容ãå¤åãããããï¼ã¦ã¼ã¶ã¼ã®ã¯ãªãã¯ã¤ãã³ããªã©ã«åºã㦠ActionCreator ã Action (åãªãJSãªãã¸ã§ã¯ã) ãçºè¡ãï¼Reducer ã¨å¼ã°ããé¢æ°ã Action ã® type ã«å¿ãã¦æ¬¡ã«ã»ãããã¹ã state (props?) ãè¿ãï¼ããã«åºãã¦ã³ã³ãã¼ãã³ãã® render é¢æ°ãå·®åãæç»ããï¼ãã£ã±ããã®ä»çµã¿ã¯é¢ç½ãï¼
ããã¾ã§ç解ãã¦ããã¨ï¼éåæã§ãªãå¦çã¯æ¸ããããªæ°ããã¦ãã¦ï¼ãã£ã¦ã¿ãã¨äºæ³ä»¥ä¸ã«ãããªãã¨å®éã«æ¸ããï¼ãããï¼ä»åã®ãé¡ã¯Gyazoã¢ã«ãã ãä½ããã¨ãªã®ã§ã©ãã㧠Gyazo API ãå¼ã¶å¿
è¦ãããï¼é©å½ã«å¼ãã§ãåãããã ãã©ï¼ããã ã¨ç¾ããæ©æ§ãå£ãã¦ãã¾ãã®ã§è²ã
調ã¹ã¦ã¿ãï¼ãã㨠Action 㨠Reducer ã®éã«åã¾ãã Middleware ã§ãã react-thunk ã使ãã¨è¯ãããããã¨ãåãã£ãï¼ãããç¨ããã¨ï¼ã¦ã§ãAPIå¼ã³åºããèªã¿è¾¼ã¿å®äºã®ç¶æ³ã«å¿ãã¦éåæçã« Action ãçºè¡ (dispatch) ãã fetchGyazoImagesAsync
ã®ãããªActionCreatorãæ¸ããããã«ãªãï¼<GyazoImageList />
ã componentDidMount ã«ãªã£ãã¨ãã«é¢æ°fetchGyazoImagesAsyncãå¼ã³åºããã¨ã§ï¼ç»é¢è¡¨ç¤ºã®ã¿ã¤ãã³ã°ã§èªç¶ã¨APIã³ã¼ã«ãå§ã¾ã£ãï¼ããã¾ã§ãã¾ãæèãã¦ããªãã£ããï¼APIãå¼ã³åºãã¦ããç®çéæããã¾ã§ã«ã¯ï¼ä»¥ä¸ã®ãããªç¶æ
ãèããããï¼
- åå¾ä¸
- -> æå
- -> 失æ
ãããã®æ
å ±ãè©°ãè¾¼ãã Action ãéä¸çºè¡ããã°ï¼ãã¼ãä¸ã®ã¢ãã¡ã¼ã·ã§ã³ãä½ãããããããããªãï¼
ç¡äºã«ç»åãªã¹ãã®åå¾ãå®äºãããï¼<GyazoImageList />
å
ã« <SquareThumbnail />
ãæ°è¦çæãã¦ããï¼ããã«ã¯ãªãã¯ã¤ãã³ããä»æãããã¨ãã¯ï¼GyazoImageList ã® props ã«äºãhandleré¢æ°ãä¸ãã¦ããï¼SquareThumbnail ã§ãã®é¢æ°ãåç
§ãããï¼ã¨ãããµãã«ãã£ã¦ãã (ãããæ£ããæ¹æ³ã§ãããã¯ã¾ã èªä¿¡ããªã)ï¼ä¸çªä¸å±¤ã¾ã§åç
§ãä¼ããã¨ï¼é¢æ° mapDispatchToProps ã§ç´ä»ãã¦ãã ActionCreator ãçºåããï¼ãã®é¢æ°ãçºè¡ãã Action ã«ãã£ã¦ï¼åçãã¬ãã¥ã¼ç¨ã®Reactã³ã³ãã¼ãã³ãã§ãã <GyazoImage />
ã® props ãæ´æ°ããã¦ï¼ç»é¢ãæ´æ°ãããï¼
å®éã«ã¯ãªãã¯ãã¦åããã¦ããæ§åã¯ãããªæãã§ï¼Chrome Devtool ã§ãã React Developer Tools ãå
¥ãã¦ããã¨ï¼ã¯ã©ã¤ã¢ã³ããµã¤ãã§æ§ç¯ãããReactã³ã³ãã¼ãã³ãã® props ã store ã®ä¸èº«ãªã©ãéä¸ç¢ºèªã§ããï¼<SquareThumbnail />
è¦ç´ ãã¯ãªãã¯ãã㨠<GyazoImage />
ã®å
容ãæ´æ°ãããï¼DevToolã§ã¯ï¼æ´æ°ãããç®æãç·ã§ãã¤ã©ã¤ããããï¼
ããã¾ã§Albumã¢ããªãæ¸ãã¦ããããã©
- props 㨠state ã®éãããã¾ã²ã¨ã¤åãããªã
- ã³ã³ãã¼ãã³ãé㧠props åã渡ããªã¬ã¼ã¿ãããªãã¨ãèµ·ãã£ã¦ãã¦é¬±é¶ããã®ã ãã©ï¼ãããªãããªã®ã ãããï¼
- props 㨠state ãç´ä»ãã¦ãã(?)
mapStateToProps
ã®æ¸ãæ¹ãã ãã¶é¢åãããããã©ï¼æ¬å½ã«ç¾ç¶ã®æ¸ãæ¹ã§åã£ã¦ããã®ã
ã®ãããªçåãæ®ã£ãï¼æ¬¡å以éã®ç·´ç¿ä½åã§è§£æ±ºãããï¼