React+Fluxã§TodoMVCãä½ã£ã¦Fluxã«ã¤ãã¦å¦ãã§ã¿ã
ã¡ãã£ã¨åã«Reactã使ã£ã¦ç°¡åãªã¢ããªã±ã¼ã·ã§ã³ãä½ã£ã¦ã¿ãã®ã§ãã
React入門用に簡単なアプリケーション作ってみる - yutaponのブログ
ä»åã¯Fluxã¢ã¼ããã¯ãã£ã«ã¤ãã¦å¦ã³ããã¨æã£ãã®ã§ãTodoMVCãé¡æã«åçµãã¦ã¿ã¾ããã
æ§æã»ãã¸ãã¯ã¯åèã«ãã¤ã¤ãES6æ§æã§æ¸ãããã«ãã¦ã¾ãã
åèã«ããã³ã¼ãã¯facebook/fluxã®examplesã®ã³ã¼ãã«ãªãã¾ãã
- flux/examples/flux-todomvc at master · facebook/flux · GitHub
- https://github.com/facebook/flux/tree/master/examples/flux-chat
ã
ä½ã£ãã³ã¼ãã¯ããã«ç½®ãã¦ãã¦ã
https://github.com/sskyu/react-flux-todomvc-example/tree/blog-20150427
åããã¨ãã¤ãã®TODOãªã¹ãã表示ããã¾ãã
Fluxã¨ã¯
ããã¾ãã«è¨ãã¨ãMVCã¢ã¼ããã¯ãã£ã¿ãããªè¨è¨ææ³ã®ãã¨ã
MVCã ã¨3ã¤ã®è²¬åã«åå²ããã¨ããããFluxã ã¨å¤§ãã4ã¤ã®è²¬åã«åå²ãã¦ãã¾ãã
å³ãè¦ã¦ããã£ãã»ããæ©ãã§ãã
ãã¡ãã«ããããè¦ãå³ã使ããã¦ããã ãã¾ããã
次ã®4ã¤ã«åå²ããã¦ããããã«è¦ãã¾ããï¼WebAPIã¯ä¸æ¦ç¡è¦ï¼
- ActionCreators
- Dispatcher
- Store
- View (React)
ããã§æ³¨ç®ãããã®ã¯ãä¾ãã°ActionCreatorsã®å ´åã¯
- Storeã¨ç´æ¥ããã¨ãããã
- Viewã¾ãã¯ActionCreatorsèªèº«ã«å¼ã³åºããã
- Dispatcherã¨ããã¨ããã
ã¨ãã風ã«ãå¦çã®æµããåæ¹åã«éå®ããã¦ãããã¨ã§ãã
ãã®èãæ¹ã«å¾ã£ã¦è¨è¨ãã¦ããã¨ãå¦çã®æµããæ´ã¿ããããªãå©ç¹ããã¾ãã
åè¦ç´ ãã©ã®ãããªè²¬åãæã£ã¦ãããã¯ãç§ãç解ããç¯å²ã ã¨æ¬¡ã®ããã«ãªãã¾ãã
ActionCreators
- ã¢ããªã±ã¼ã·ã§ã³å ã®DSLã®ãããªãå®è£ ã¨ã¯ä¸ã¤åãé¢ããæ½è±¡çãªå½ä»¤ã»ãããå®ç¾©ããã
- Viewï¼ã¦ã¼ã¶ã¼ï¼ããã®ã¢ã¯ã·ã§ã³ã¨ããã¼ã¿ãåå¾ã»ä¿æããããã®APIã¢ã¯ã·ã§ã³ããã
- å¤é¨ã®APIãå©ãæã¯Storeã§ã¯ãªãActionCreatorså
ã®è²¬å
- ã³ã¼ããç ©éåããã®ã§å¤é¨APIã使ãæã¯ApiUtilsãå®ç¾©ããã¨è¯ã
Dispatcher
- ActionCreatorsãDispatcherã«ã¢ã¯ã·ã§ã³ãpublishãã
- StoreãDispatcherã®ã¢ã¯ã·ã§ã³ãsubscribeãã
- å®è£ ã®éãå°ãªããåé·ãã¨æããããããªããã©ãå®è¡ãããã³ã¼ã«ããã¯ã®é çªãå¶å¾¡ãããããã®ã«å¿ è¦
Store
- ãªã½ã¼ã¹ç®¡çã¨ãã¸ãã¯é¨å
- Dispatcherã«publishãããã§ãããã¤ãã³ããè³¼èªãã¦ãåã¢ã¯ã·ã§ã³ã®ãã³ããªã³ã°ãã
- ã·ã³ã°ã«ãã³ã¨ãã¦å®è£
ãããµã¼ãã¼ã¨åããã¼ã¿ããã¼ã«ã«å
ã«ãä¿æãã¦ãã
- ãªã½ã¼ã¹ç¶æ ãæä¾ããgetterã¡ã½ããã¯å®ç¾©ãã¦ããããããªã½ã¼ã¹ãæ´æ°ããsetterã¡ã½ããã¯æä¾ããªã
- Viewå´ããç´æ¥ãªã½ã¼ã¹ãå¤æ´ã§ããªããããæ´æ°ç³»ã¯Dispatcherçµç±ã®ãã³ãã©ã§å¦çãã
- ä¿æãã¦ãããã¼ã¿ã«å¤æ´ããã£ãå ´åã¯EventEmitterçµç±ã§changeã¤ãã³ããçºè¡ãã
View
- UIé¨åå ¨è¬ãæ å½
- ControllerViewçãªã³ã³ãã¼ãã³ããStoreããæç»ã«å¿ è¦ãªãã¼ã¿ãåãåããæç»å¾ããã¼ã¿ã®å¤æ´ãç£è¦ãã
- ãã¼ã¿ã®å¤æ´ããã£ãå ´åã¯å·®åã ããé¨åæç»ãããï¼virtual-domã®ãããï¼
- ã¦ã¼ã¶ããã®ã¢ã¯ã·ã§ã³ãããã°ãã³ããªã³ã°ããå¿ è¦ãªãActionCreatorsçµç±ã§ãã¼ã¿ãæä½ãã
ControllerViewçãªã³ã³ãã¼ãã³ãã¨ããã®ã¯ãä»åã®ä¾ã§è¨ãã¨components/TodoApp.js
ã«ãªãã¾ãã
ããã¤ãStoreãsubscriibeãã¦ãå¤æ´ãããã° setState()
ã§ã³ã³ãã¼ãã³ããæã¤ç¶æ
ãææ°ã«ãã¦render()
ãèµ°ããã¨ãã£ãæµãã«ãªãã¾ãã
ã
ã¾ã¨ãã¦ã¿ãã¨ãããªæãã§ç解ãã¾ããã
TodoMVCåçµãã¦ã¿ã¦ã®ææ
Reactï¼virtual-domï¼ã«ããæç»ã³ã¹ããèããªãã¦ãè¯ãããã°ã©ãã³ã°ã«ææ¿ãã¾ããã
Storeãè¦ãã¨ãããã®ã§ãããTodoã®éåãStoreå ã§ããã¢ã¯ã»ã¹åºæ¥ãªãå¤æ°ã«ä¿æãã¦ã常ã«ãµã¼ãã¼ï¼å®ä½ã¯localStrageï¼ã¨åãç¶æ ãä¿ã¤ããã«ãã¦ãã¾ãã
ä¾ãã°ã¦ã¼ã¶ãæ¢åã®Todoã®ããã¹ããå¤æ´ããã¨æ¬¡ã®ãããªå¦çãèµ°ãã¾ãã
- ãViewãããã¹ãããã¯ã¹ã®ç·¨éå®äºãæ¤ç¥ãã¦UPDATE_TEXTã¢ã¯ã·ã§ã³ãèµ°ã
- ãActionãUPDATE_TEXTã¢ã¯ã·ã§ã³ãå®è¡
- ããã«APIã«æ´æ°å¦çãæãã
- ãDispatcherãUPDATE_TEXTã¢ã¯ã·ã§ã³ãå®è¡ããããã¨ãStoreã«éç¥
- ãStoreãUPDATE_TEXTã¢ã¯ã·ã§ã³ããã³ããªã³ã°
- APIã®æ´æ°å®äºãå¾ ããã«å¯¾è±¡ã®Todoãæ´æ°ãã
- changeã¤ãã³ãçºè¡
- ãViewãStoreã®ãã¼ã¿å¤æ´ãæ¤åºãã¦Todosãæç»
- æ´æ°ãåæ ããã¦ãã
- ãAPIãæ´æ°å®äºãActionã使ã£ã¦SYNC_TODOSã¢ã¯ã·ã§ã³ãå¼ã¶
- ãDispatcherãSYNC_TODOSã¢ã¯ã·ã§ã³ãå®è¡ããããã¨ãStoreã«éç¥
- ãStoreãSYNC_TODOSã¢ã¯ã·ã§ã³ã«ãã£ã¦æ¸¡ãããTodosãStoreèªèº«ãä¿æãã¦ããTodosã«ä¸æ¸ã
- Storeãchangeã¤ãã³ãçºè¡
- ãViewãStoreã®ãã¼ã¿å¤æ´ãæ¤åºãã¦Todosãæç»
- å¤æ´ããã£ãããã¹ããæã¤DOMãã¼ãã®ã¿ãåæç»ããã
ããã®ä¸ã ãè¦ãã¨å¦çã®æµããä¸æ¹åã§ç§©åºãããæãããã¾ããã»ã»ï¼
ã
ãã¤ã³ãã¯APIã®æ´æ°å¦çãå¾
ããã«è¡¨ç¤ºãå¤æ´ããããµã¼ãã¼ã¨åæãããããä¸åº¦Viewãæ´æ°ãç´ãã¨ããã§ãã
ï¼ã¦ã¼ã¶ã¼ã«ãã¡æ©ããã£ã¼ãããã¯ãè¿ãããã«ãã®ãããªä»æ§ã«ãã¦ã¾ãã
ããµã¼ãã¼ã®æ´æ°å¦çãå¾
ã£ã¦è¡¨ç¤ºãããå ´åã¯ãã¼ãã£ã³ã°ç»é¢ã表示ãã¦ãè¯ãã§ããããï¼
ããBackboneã§æç»ã³ã¹ããèæ ®ãã¦ãããã¨ããã¨ãååã®å¤ã¨æ¯è¼ãã¦å¤æ´ããã¦ããé¨åã ããæ´æ°ããã»ã»ã¨ãã£ããã§ãã¯ããã³ã¼ããå¢ãã¦ãããã©ãã©ãã³ã¼ããç ©éåãã¦ããã¾ãã
Reactã ã¨ãã®è¾ºã®æç»ã³ã¹ããvirtual-domä»»ãã«ã§ããã®ã§ãæç»ã³ã¹ãããã¾ãèããªãã¦ãè¯ããªãã¾ãã
Babelã«ã¤ãã¦
Babelããããè¯ãã¦ãå°æ¥çã«jQueryã¿ãããªãã¸ã·ã§ã³ã«ãªã£ã¦å«ãããªããå¿é ã
ãã¤ã®éã«ãClassæ§æã§ã¯ã©ã¹ã®memberãå®ç¾©ã§ããããã«ãªã£ã¦ã¾ããã
export default class TodoItem extends React.Component { // static member static propTypes = { todo: React.PropTypes.object.isRequired }; // instance member state = { isEditing: false }; // 以ä¸ç¥ }
ãã®æ©è½ã¯ããã©ã«ãã§ç¡å¹åããã¦ããï¼ES6ä»æ§ã«ãªãï¼ã®ã§ãbabelã«ãªãã·ã§ã³ã¨ãã¦stage: 0
ã¿ãããªãã®ãå ããå¿
è¦ãããã¾ãã
詳ããã¯ãã¡ããåç §ãã¦ä¸ããã
https://babeljs.io/docs/usage/experimental/
Babelã使ã£ã¦ããã°ããã©ã«ãã§FlowTypeã使ããã®ã§ããããã使ã£ã¦ãã¾ãã¨Babelã«ããã¯ã¤ã³ãããã¦ãã¾ãæ¸å¿µããã£ããï¼æé ãï¼ã
React+Fluxã®ã¾ã ããããªãã¨ãã
ãã¡ã¤ã«ã®å½åè¦å
JSX使ã£ã¦ããã³ã³ãã¼ãã³ãã¨ãã¯.jsx
ã«ãããã¨ãã¯ããããã
ããããè¦ã¦åã£ããã©å
é 大æåã®ãã¡ã¤ã«åã§å§ããã®ãå¤ãã£ãã
Constantsã¨ããªãã¸ã§ã¯ãã®åç
§ãexportãã¦ããã¡ã¤ã«ãå
é 大æåã®ãã®ãå¤ããå人çã«ã¯ã¯ã©ã¹å®ç¾©ãexportãã¦ããã¤ã ãå
é 大æåãããã¨æã£ã¦ããã©ãéãã®ã ãããã
ãããããã£ã¦æ確ãªæ±ºã¾ãããªãã¿ãããªã®ã§ãããã¸ã§ã¯ãå ã§ã¹ã¿ã¤ã«ã¬ã¤ãä½ã£ã¦è¡ãã°è¯ãããã
ã³ã³ãã¼ãã³ããå¤ããªã£ã¦ããã¨ãã®ãã£ã¬ã¯ããªæ§æ
å®å ¨ã«ç¬ç«ããã³ã³ãã¼ãã³ããªãcomponentsãã£ã¬ã¯ããªä»¥ä¸ã«ãã©ããã«ç½®ãã¦ãããã®ããªã
ããã³ã³ãã¼ãã³ãã«å¾å±ããã³ã³ãã¼ãã³ãã¨ãã¯ãã£ã¬ã¯ããªåããããªããã©ãããã«ããå¼å®³ã¿ãããªã®ããã®ã ãããã
ããã¡ãã£ã¨å¤§ãããã®ã¤ãã£ã¦ã¿ãªãã¨å®æãããªãã¨ããã§ããã
Storeã·ã³ã°ã«ãã³åé¡
ä»åã¯TodoStore
ã¯ã©ã¹ãä½ã£ã¦ããã®ä¸ã«Actionã®ãã³ããªã³ã°ã¨ãããããè©°ãè¾¼ãã§ãexport default new TodoStore()
ã¿ããã«ã¤ã³ã¹ã¿ã³ã¹ã®åç
§ãå¤ã«åºããã
ããã¢ããªã±ã¼ã·ã§ã³ãSPAã§ã¹ããã対象ã«ããå ´åãã¡ã¢ãªç®¡çãå¾¹åºãã¦ããå¿ è¦ãããããã ãã©ããã®ã¾ã¾ã ã¨ã¤ã³ã¹ã¿ã³ã¹ã®åç §ãã©ã®ã¿ã¤ãã³ã°ã§ç ´æ£ããã°ãããããããªããã¨ã«ãªã£ã¦ãã
ãã¡ãã®ã¹ã©ã¤ããè¦ã¦ãStoreã¯ã·ã³ã°ã«ãã³ã§ããããã ã£ã¦æã£ãããããã©ãæ¬å½ã®ã¨ããã¯ã©ããªã®ã ãããã
StoreManagerã¿ãããªãã¤ãä¸ä½ã«è¨ãã¦ã¤ã³ã¹ã¿ã³ã¹ã®ç®¡çãããã»ããè¯ãæ°ãããã
ããã«ãã¦ããã®ã¹ã©ã¤ãããã£ã¡ãããããããã
ã
ãããã«
ReactMeetup#1ã§azuãããè¨ã£ã¦ãæ°ããããã©ã
Fluxãç解ããã«ã¯å®éã«æ¸ãã¦ã¿ãã®ãä¸çªã£ã¦ã®ã¯æ¬å½ã ãªã¨æãã¾ãã
ä¸åæ¸ãã¦ã¿ãã¨ãæåã¯åé·ã ã¨æã£ã¦ããæ¸ãæ¹ã秩åºãæã¡å§ãããã¨ã«æ°ã¥ãã¾ããã
ããã¦ActionCreatorsãã¢ããªã±ã¼ã·ã§ã³ã®ä¸»å½¹ã«è¦ãã¦ãã¾ãã
ç´°ããé¨åã§ã¾ã çåç¹ãããã®ã§ããã¦ãã¦ããã£ã¨ç¥ãããã¨ããã