å½ããã°ã®è¨äºã§Reactå¦ç¿ããã¦ããèªè æ§ãããã§ãªãåå¿è æ§ãReactãå¦ã³å§ãã¦ãReduxã¨ããç¨èªãä½åº¦ãèãããã¨ãããããããã¾ããã
ç¹ã«Webéçºã®ä¸çã«ä¸æ £ããªæ¹ã¯ãååãèãããã¨ãããããããã¾ããããã»ã¨ãã©ã®æ¹ã ã¯ã Reduxã¨ã¯ä½ã§ãã? ãã¨çåã«æã£ã¦ããæ¹ãå¤ãããã£ããããã¨æãã¾ãã
ããã解決ããä»çµã¿ã¨ä½¿ãæ¹ãå¦ãã§ããã¾ãããã
ãã®è¨äºã§ã¯ãReduxã¨ã¯ä½ãã説æãããã®ä¸»ãªæ©è½ã«ã¤ãã¦èª¬æãã¾ãã
Reduxã¨ã¯ï¼
Reduxã¯ãReactã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ 管çã©ã¤ãã©ãªã§ãããã¢ããªã±ã¼ã·ã§ã³å ã®ç¶æ ãããå¹ççã«ç®¡çããããã«ä½¿ç¨ããã¾ãã
æ¯æã«ä½ç¾ã»ä½ä¸åããã¦ã³ãã¼ãããããã¹ã¦ã®Reactããã¸ã§ã¯ãã®50ï¼ ä»¥ä¸ãããã使ç¨ãã¦ãã¾ãã
ã»ã¨ãã©ã®Reactéçºè ã«ãã£ã¦ãWebã¢ããªã¨ã¢ãã¤ã«ã¢ããªã®ä¸¡æ¹ã§ã®å°è¦æ¨¡ããã³å¤§è¦æ¨¡ãªReactããã¸ã§ã¯ãã«æ¡ç¨ããã¦ããã¾ãã
è¦ããã«ãReduxã¯Reactã§ã®ç¶æ ã管çããæ¹æ³ã§ãããæ§é åãããæ¹æ³ã§ãã¹ã¦ã®ã³ã³ãã¼ãã³ãããã¢ã¯ã»ã¹ã§ãããã£ãã·ã¥ã¾ãã¯ã¹ãã¬ã¼ã¸ã§ããã¨ãè¨ãã¾ãã
ç´æ¥DOMã¬ã³ããªã³ã°ãåé¤ããç¶æ ã1ãæã§ç®¡çããã®ã«å½¹ç«ã¤ãã®ã§ãã
ãªããReduxãªã®ãï¼
ç¶æ ã¯ãå¤ãã®å ´åããã¼ã¿ã表示ããã³ã³ãã¼ãã³ãã§ç´æ¥ç®¡çããã¾ãã
ãã¨ãã°ãã³ã³ãã¼ãã³ãã¯ãã©ã¼ã å ¥åã®ç¾å¨ã®å¤ãç¬èªã®ç¶æ ã«ä¿åããã¦ã¼ã¶ã¼ã®å ¥åã«å¿ãã¦ãã®ç¶æ ãæ´æ°ããå ´åãããã¾ãã
ã¢ããªã±ã¼ã·ã§ã³ã®ãµã¤ãºã¨è¤éããå¢ãã«ã¤ãã¦ããã®ç¶æ ã®ç®¡çãé£ãããªãå¯è½æ§ãããã¾ãã
ã¤ã¾ãReactã§ã¯ããã¼ã¿ãã©ã®ã³ã³ãã¼ãã³ãããæ¥ã¦ãããã追跡ããã®ãé£ãããããã³ã³ãã¼ãã³ãéã®ç¶æ 転éã¯ããªãé¢åã§ãã
ã¦ã¼ã¶ã¼ãã¢ããªã±ã¼ã·ã§ã³å ã§å¤æ°ã®ç¶æ ãæä½ãã¦ããå ´åãé常ã«è¤éã«ãªãã¾ãã
ããã§Reduxã®åºçªã§ãã
Reduxã¯ãã¹ãã¢ã¨å¼ã°ãã1ã¤ã®å ´æã«ãã¹ã¦ã®ç¶æ ãæ ¼ç´ãããã¨ã§ãç¶æ 転éã®åé¡ã解決ãã¾ãã
ãã®ããããã¹ã¦ã®ç¶æ ãåãå庫(ã¹ãã¢)ã«æ ¼ç´ããããããç¶æ ã®ç®¡çã¨è»¢éã容æã«ãªãã¾ãã
ã¢ããªã±ã¼ã·ã§ã³å ã®ãã¹ã¦ã®ã³ã³ãã¼ãã³ãã¯ããã®ã¹ãã¢ããå¿ è¦ãªç¶æ ã«ç´æ¥ã¢ã¯ã»ã¹ã§ãã¾ãã
ããã ãã§ã¯ããã¾ãã¡åãããªãã¨æãã¾ãã®ã§ãããã詳ãã解説ãã¦ããã¾ãã
Reduxã®ä»çµã¿
Reduxã®ä»çµã¿ã¯é£ããäºã§ã¯ãªãç°¡åã§ãã
åã³ã³ãã¼ãã³ãã¯ãããã³ã³ãã¼ãã³ãããå¥ã®ã³ã³ãã¼ãã³ãã«Propsãéããã¨ãªããä¿åãããç¶æ ã«ã¢ã¯ã»ã¹ã§ãã¾ãã
ã¤ã¾ããä¸è¨ã®3ã¤ã®æãéè¦ãªReduxã®ååãããã¾ãã
Reduxã®åºæ¬çãªæ§æè¦ç´ ã¯ä¸è¨ã¨ãªãã¾ãã
ã» Store(ã¹ãã¢)
ã» action(ã¢ã¯ã·ã§ã³)
ã» reducer(ã¬ãã¥ã¼ãµã¼)
Reduxã¯ãã¢ããªã±ã¼ã·ã§ã³å ã®ç¶æ ãåä¸ã®ã¹ãã¢ã«æ ¼ç´ããã¢ããªã±ã¼ã·ã§ã³å ã®ãã¹ã¦ã®ã³ã³ãã¼ãã³ãããã®ã¹ãã¢ã«ã¢ã¯ã»ã¹ã§ããããã«ãã¾ãã
ã³ã³ãã¼ãã³ãã¯ãã¹ãã¢å ã®ç¶æ ãæ´æ°ããããã®ã¢ã¯ã·ã§ã³ããã£ã¹ããããããã¨ãã§ãã¾ãã
ã¹ãã¢ã¯ãã¢ã¯ã·ã§ã³ãåãåããããã«å¿ãã¦ç¶æ ãæ´æ°ããããã®ã¬ãã¥ã¼ãµã¼ãå¼ã³åºãã¾ãã
ãã®ã¢ã¼ããã¯ãã£ã¯ãã¢ããªã±ã¼ã·ã§ã³å ã®ç¶æ å¤æ´ã追跡ãã管çãããã¨ãç°¡åã«ãã¾ãã
éè¦ãªã®ããã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ å ¨ä½ãä¿æããä¸å¤®ã¹ãã¢ã§ãã
Reduxãã¼ã«ã«ç¿çããã«ã¯ããããã®ã³ã³ãã¼ãã³ãã¾ãã¯åºæ¬çãªæ§æè¦ç´ ã«ã¤ãã¦ã®ååãªç¥èãå¿ è¦ã¨ãªãã¾ãã
ãããããä½ãããã®ãç°¡åã«èª¬æãã¦ããã¾ãã
Reduxã®ã¢ã¯ã·ã§ã³
Reduxã®ã¢ã¯ã·ã§ã³(action)ã¯ãã¢ããªã±ã¼ã·ã§ã³å ã§çºçããã¤ãã³ããç¶æ å¤æ´ã®ããã®ãªãã¸ã§ã¯ãã§ããããã®ã¤ãã³ããå¤æ´ã説æããããã«ã¿ã¤ã(type)ããããã£ãæã¡ã¾ãã
ã¾ããä»»æã®è¿½å ã®ãã¼ã¿ãå«ãããã¨ãã§ãã¾ãã
Reduxã®ã¢ã¯ã·ã§ã³ã¯ãã¢ããªã±ã¼ã·ã§ã³å ã®ããããå ´æãããã£ã¹ããã(dispatch)ããã¾ãã
ã¢ã¯ã·ã§ã³ããã£ã¹ããããããã¨ã§ãReduxã¹ãã¢ã«å¯¾ãã¦å¤æ´ããªã¯ã¨ã¹ãããã¹ãã¢å ã®ç¶æ ãå¤æ´ãããã¨ãã§ãã¾ãã
ã¢ããªã±ã¼ã·ã§ã³ããReduxã¹ãã¢ã«ãã¼ã¿ãéä¿¡ã§ããå¯ä¸ã®æ¹æ³ã¨ãªã£ã¦ãã¾ãã
ãã¨ãã°ãã«ã¦ã³ã¿ã¼ã¢ããªã®å ´åãã¤ã³ã¯ãªã¡ã³ããã¿ã³ãã¯ãªãã¯ãããå ´åãã¹ãã¢å ã®ã«ã¦ã³ã¿ã¼ã®å¤ãå¢ããããã®ã¢ã¯ã·ã§ã³ãä½æãããã¨ãã§ãã¾ãã
ãã®ã¢ã¯ã·ã§ã³ã¯ãä¸è¨ã®ããã«ãªãã¾ãã
{ type: 'INCREMENT' }
ãã®ã¢ã¯ã·ã§ã³ã¯ãReduxã¹ãã¢ã«éä¿¡ãããã¹ãã¢ã¯ãã¢ããªã±ã¼ã·ã§ã³å ã§å®ç¾©ãããReducerã«ãã£ã¦å¦çããã¾ãã
Reducerã¯ãç¾å¨ã®ç¶æ ã¨ã¢ã¯ã·ã§ã³ã使ç¨ãã¦ãæ°ããç¶æ ãçæããããã®ç´ç²ãªé¢æ°ã§ãã
ã¢ã¯ã·ã§ã³ãReducerã«ãã£ã¦å¦çãããã¨ãã¹ãã¢å ã®ç¶æ ãæ´æ°ãããã¢ããªã±ã¼ã·ã§ã³ã®ãã¥ã¼ã«åæ ããã¾ãã
Reduxã¢ã¯ã·ã§ã³ã®å½¹å²ã¯ä»¥ä¸ã®éãã§ãã
ã» ã¢ããªã±ã¼ã·ã§ã³å ã§èµ·ãã£ãã¤ãã³ããè¨é²ãã
Reduxã®ã¢ã¯ã·ã§ã³ã¯ãã¢ããªã±ã¼ã·ã§ã³å ã§ä½ãèµ·ãã£ãã®ããè¨é²ããããã«ä½¿ç¨ããã¾ããä¾ãã°ãã¦ã¼ã¶ã¼ãããã°ã¤ã³ããã¿ã³ãã¯ãªãã¯ããå ´åããã°ã¤ã³ã¢ã¯ã·ã§ã³ãçºçãã¾ãã
ã» ã¹ãã¢ã«ãã¼ã¿ãéä¿¡ãã
ã¢ã¯ã·ã§ã³ã¯ãã¹ãã¢ã«ãã¼ã¿ãéä¿¡ããããã«ä½¿ç¨ããã¾ããä¾ãã°ãæ°ããã¿ã¹ã¯ã追å ãããå ´åãã¢ããªã±ã¼ã·ã§ã³ã¯ã¿ã¹ã¯è¿½å ã¢ã¯ã·ã§ã³ããã£ã¹ãããããã¹ãã¢ã«ã¿ã¹ã¯ã追å ããããæ示ãã¾ãã
ã» ã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ ãå¤æ´ãã
Reduxã¢ã¯ã·ã§ã³ã¯ãã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ ãå¤æ´ããããã«ä½¿ç¨ããã¾ããã¢ã¯ã·ã§ã³ã¯ãã¹ãã¢å ã®ç¶æ ãæ´æ°ããæ°ããç¶æ ãçæããããã«æ示ãã¾ãã
ã» ã¢ããªã±ã¼ã·ã§ã³å ã®ãã¹ã¦ã®ã³ã³ãã¼ãã³ãã«éç¥ãã
ã¢ã¯ã·ã§ã³ã¯ãã¢ããªã±ã¼ã·ã§ã³å ã®ãã¹ã¦ã®ã³ã³ãã¼ãã³ãã«éç¥ããããã«ä½¿ç¨ããã¾ãã ã¢ã¯ã·ã§ã³ããã£ã¹ããããããã¨ãã¹ãã¢ã¯ãµãã¹ã¯ã©ã¤ãããããã¹ã¦ã®ã³ã³ãã¼ãã³ãã«éç¥ããå¤æ´ãåæ ããããã«æ示ãã¾ãã
ã¢ã¯ã·ã§ã³ã§å¦çãããã¹ãæ å ±ãå«ãpayload(ãã¤ãã¼ã)ãæããªããã°ãªãã¾ããã
ã¢ã¯ã·ã§ã³ã¯ãã¢ã¯ã·ã§ã³ã¯ãªã¨ã¤ã¿ã¼ã«ãã£ã¦ä½æããã¾ãã
ä¾ãã°ãåè¿°ããããã«ã¢ããªã®ãã°ã¤ã³ãªã©ã§ã®å ´åã§ã¯ä¸è¨ã®ããã«ãã¾ãã
const loginStatus = (name, password) => { { type: "LOGIN", payload: { username: "Taro", password: "xxxx" } } }
ã¢ã¯ã·ã§ã³ã¯type
ããããã£ãå«ãå¿
è¦ããããæ ¼ç´ãããä»ã®ãã¤ãã¼ããå«ãå¿
è¦ãããã¾ãã
ã¤ã¾ããã¢ã¯ã·ã§ã³ã¯ã¢ããªã±ã¼ã·ã§ã³ããã¹ãã¢ã«ãã¼ã¿ãéä¿¡ããæ å ±ã®ãã¤ãã¼ãã¨ãªãã¾ãã
æãã¦ããã¹ãäºã¯ããã¹ã¦ã®ãã¼ã¿ã¯æçµçã«ã¢ã¯ã·ã§ã³ã¨ãã¦ãã£ã¹ããããããå¿ è¦ãããäºã§ãã
Reduxã®ã¢ã¯ã·ã§ã³ã¯ãã¢ããªã±ã¼ã·ã§ã³å ã®ç¶æ 管çã«ããã¦é常ã«éè¦ãªå½¹å²ãæããã¾ãã
ã¢ããªã±ã¼ã·ã§ã³å ã§ä½ãèµ·ãã£ãã®ããæ確ã«ç¤ºããç¶æ å¤æ´ã管çããããã«å¿ è¦ãªãã¹ã¦ã®æ å ±ãæä¾ãã¾ãã
Reduxã®ãã£ã¹ããã
Reduxã®ãã£ã¹ããã(dispatch)ã¯ãã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ (state)ãå¤æ´ããããã«ä½¿ç¨ãããé¢æ°ã§ãã
ãã£ã¹ãããã使ç¨ãããã¨ã§ãã¢ããªã±ã¼ã·ã§ã³å ã®ã©ãããã§ããã¢ã¯ã·ã§ã³(action)ãä½æããã¹ãã¢(store)ã«éä¿¡ãããã¨ãã§ãã¾ãã
import { createStore } from 'redux' // ã¢ã¯ã·ã§ã³typeã®å®ç¾© const INCREMENT = 'INCREMENT' const DECREMENT = 'DECREMENT' // ã¢ã¯ã·ã§ã³ã¯ãªã¨ã¼ã¿ã¼ã®å®ç¾© function increment() { return { type: INCREMENT } } function decrement() { return { type: DECREMENT } } // ã¬ãã¥ã¼ãµã¼ã®å®ç¾© function counterReducer(state = 0, action) { switch (action.type) { case INCREMENT: return state + 1 case DECREMENT: return state - 1 default: return state } } // ã¹ãã¢ã®ä½æ const store = createStore(counterReducer) // ãã£ã¹ãããã使ã£ã¦ã¢ã¯ã·ã§ã³ãéä¿¡ãã store.dispatch(increment()) // ç¶æ ã1å¢ãã store.dispatch(decrement()) // ç¶æ ã1æ¸ãã
ä¸è¨ã®ä¾ã§ã¯ãstore.dispatch()
ã使ç¨ãã¦ãincrement()
ã¢ã¯ã·ã§ã³ããã³decrement()
ã¢ã¯ã·ã§ã³ãã¹ãã¢ã«éä¿¡ãã¦ããã¾ãã
ã¹ãã¢ã¯ãã¢ã¯ã·ã§ã³ã«åºã¥ãã¦ç¶æ ãå¤æ´ããæ°ããç¶æ ãçæãã¾ãã
ãããã®ã¢ã¯ã·ã§ã³ãã¹ãã¢ã«éä¿¡ãããçµæãç¶æ ã1å¢ããå¾ã«1æ¸ãã¾ãã
ã¤ã¾ããã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ ãæ´æ°ãããã¨ãã¯ãã¤ã§ããããããStoreã«ãã£ã¹ããã(éä¿¡)ããã ãã§ãã
æ®ãã¯ã¬ãã¥ã¼ãµã¼ã«ãã£ã¦å¦çããã¾ãã
Reduxã®reducer
Reducer(ã¬ãã¥ã¼ãµã¼)ã¯ãã¢ããªã±ã¼ã·ã§ã³ã®ç¾å¨ã®ç¶æ (state)ãåå¾ããã¢ã¯ã·ã§ã³(action)ãå®è¡ãã¦ããæ°ããç¶æ ãè¿ãç´ç²é¢æ°ã¨ãªãã¾ãã
è¨ãæãã¾ãã¨ãç¾å¨ã®ç¶æ ã¨ã¢ã¯ã·ã§ã³ãåãå ¥ããå®è¡ãããã¢ã¯ã·ã§ã³ã¨ã¨ãã«æ°ããç¶æ ãè¿ãé¢æ°ã§ãã
ããã¦ç¶æ ã¯ãªãã¸ã§ã¯ãã¨ãã¦ä¿åããã¾ãã
ãªãã¸ã§ã¯ãã¨ãã¦ä¿åããã¹ãã¢ã«éä¿¡ãããã¢ã¯ã·ã§ã³ã«å¿çãã¦ã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ ãã©ã®ããã«å¤åããããæå®ãã¾ãã
Reducerã¯JavaScriptã®reduce
é¢æ°ããåç¨ãã¦ãã¾ããã¤ã¾ãreduce
é¢æ°ããã¼ã¹ã¨ãªã£ã¦ãã¾ããã³ã¼ã«ããã¯é¢æ°ãå®è¡ãããå¾ã«è¤æ°ã®å¤ããä¸ã¤ã®å¤ãè¨ç®ãã¾ãã
Array.prototype.reduce(reducer, ?initialValue)
ã«æ¸¡ããããªé¢æ°ãªã®ã§ãããã¯ã¬ãã¥ã¼ãµã¼(reducer)ã¨å¼ã°ãã¾ãã
åç´ãªã«ã¦ã³ã¿ã¼ã¢ããªã®Reducerã®ä¾ãä¸è¨ã«ç¤ºãã¾ãã
// Reducerã®å®ç¾© const counterReducer = (state = 0, action) => { switch(action.type) { case 'INCREMENT': return state + 1; case 'DECREMENT': return state - 1; default: return state; } }
ä¸è¨ã®Reducerã¯ãç¾å¨ã®ã«ã¦ã³ã¿ã¼ã®ç¶æ ã表ãæ´æ°å¤ãåãåãã¾ãã
ã¢ã¯ã·ã§ã³ãINCREMENTã¾ãã¯DECREMENTã§ããå ´åãReducerã¯ç¾å¨ã®ç¶æ
ã«1
ã¾ãã¯-1
ãå ç®ããæ°ããç¶æ
ãè¿ãã¾ãã
ãã以å¤ã®å ´åãReducerã¯ç¾å¨ã®ç¶æ ããã®ã¾ã¾è¿ãã¾ãã
Reducerã使ç¨ãããã¨ã§ãç¶æ ã®å¤æ´ã«é¢ãããã¸ãã¯ãæ確ã«ããã¢ããªã±ã¼ã·ã§ã³å ã®ç¶æ ã®å¤åãç°¡åã«è¿½è·¡ã§ããããã«ãªãã¾ãã
Reducerãæ±ãä¸ã§ã®æ³¨æç¹ãããã¾ãã
ããã¯ãReducerã®å é¨ã§çµ¶å¯¾ã«ãã£ã¦ã¯ãããªããã¨ã§ãã
ã» Reducerã®å¼æ°ãå¤æ´ãã
ã» ãã¼ã¿ãã¼ã¹å¼ã³åºããAPIå¼ã³åºããã«ã¼ãã£ã³ã°é·ç§»ãªã©ã®ãµã¤ãã¨ãã§ã¯ããå®è¡ããã
ã» ç´ç²ã§ãªãé¢æ°ãå¼ã³åºããªããã¨ãä¾ãã°Date.now()
ãMath.random()
ãªã©
ã¤ã¾ãã常ã«æ°ããç¶æ ãè¿ãç´ç²ãªé¢æ°ã§ããå¿ è¦ãããã¾ãã
Reducerãå¤é¨ã®ãã¼ã¿ã«ä¾åããããå¯ä½ç¨ãæã£ããããå ´åãReduxã¹ãã¢ã®äºæ¸¬ä¸å¯è½ãªåä½ãå¼ãèµ·ããå¯è½æ§ãããã¾ãã
使ç¨ããä¸ã§ã®å¿ ãæèãã注æç¹ã¯ä¸è¨ã¨ãªãã¾ãã
ã» ã¹ãã¬ããæ¼ç®åã使ç¨ãããã¨
Reducerãæ°ããç¶æ ãè¿ãå ´åãå¤ãç¶æ ãå¤æ´ããã®ã§ã¯ãªããã¹ãã¬ããæ¼ç®å(â¦)ã使ç¨ãã¦æ°ããç¶æ ãä½æããå¿ è¦ãããã¾ãã
ãã®çç±ã¯ãå¤ãç¶æ ãç´æ¥å¤æ´ããã¨ãReduxã¹ãã¢ã®ä¸æ£ç¢ºãªåä½ãå¼ãèµ·ããå¯è½æ§ãããçºã§ãã
ã» è¤æ°ã®Reducerã使ç¨ãããã¨
ã¢ããªã±ã¼ã·ã§ã³ãè¤éã«ãªãã«ã¤ãã¦ãReducerãè¤æ°ä½æããã¢ããªã±ã¼ã·ã§ã³å ã®è¤æ°ã®ç¶æ ã管çããå¿ è¦ã常ã«ããã¾ãã
Reducerã¯ãåãã¢ã¯ã·ã§ã³ã«å¯¾ãã¦ç°ãªãç¶æ ãè¿ããã¨ãã§ããè¤æ°ã®Reducerãçµã¿åããã¦è¤éãªã¢ããªã±ã¼ã·ã§ã³ç¶æ ãæ§ç¯ãããã¨ãã§ãã¾ãã
㻠大è¦æ¨¡ãªã¢ããªéçºã§ã¯ãå°ããªé¢æ°ã«åå²ãããã¨
大è¦æ¨¡ãªã¢ããªã±ã¼ã·ã§ã³ã®å ´åãReducerã¯è¨å¤§ãªéã®ã³ã¼ãã«ãªããä¿å®ãå°é£ã«ãªãå¯è½æ§ãããã¾ãã
ãã®å ´åãReducerãå°ããªé¢æ°ã«åå²ããã¢ã¸ã¥ã¼ã«åãããã¨ãæã¾ããã§ãã
ãããã®æ³¨æç¹ãå®ããã¨ã§ãReduxã®Reducerãå¹æçã«ä½¿ç¨ãããã¨ãã§ãã¾ãã®ã§è¦ãã¦ããã¦ä¸ããã
Reduxã®Store
ã¹ãã¢ã¯ãã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ ãä¿æãã¾ãã
ã©ã®Reduxã¢ããªã±ã¼ã·ã§ã³ã§ãã¹ãã¢ã¯1ã¤ã ãã«ãã¦ãããã¨ãå¼·ãæ¨å¥¨ããã¦ãã¾ãã
ã¤ã¾ãã Reduxã¢ããªã«ã¯1ã¤ã®ã¹ãã¢ã®ã¿ãåå¨ããå¿ è¦ãããã¨ããäºã§ãã
ç¶æ ã®ä¿æã¯ããªãã¸ã§ã¯ãã¨ãªã£ã¦ãã¾ãã
å è¿°ã§èª¬æãã¦ãããããããã¾ã¨ãããªãã¸ã§ã¯ãã«ãªãã¾ãã
ã¹ãã¢ã«ã¯ãç¶æ ãæ´æ°ããããã®ã¡ã½ãããå«ã¾ãã¦ããã¾ãã
ä¿åãããç¶æ ã«ã¢ã¯ã»ã¹ããããç¶æ ãæ´æ°ããããã¡ã½ããã使ã£ã¦ãªã¹ãã¼ãç»é²ããã解é¤ããããããã¨ãå¯è½ã§ãã
ã¹ãã¢ã®ä¸»ãªå½¹ç®ã¯ä¸è¨ã®éãã§ãã
ã» ç¶æ ã®ä¿æ
ã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ ãä¿æããReduxã¢ããªã±ã¼ã·ã§ã³ã®ä»ã®é¨åããã¢ã¯ã»ã¹ã§ããããã«ãã¾ãã
ã» ã¢ã¯ã·ã§ã³ã®åãåã
ã¢ããªã±ã¼ã·ã§ã³å ã§çºçããã¢ã¯ã·ã§ã³ãåãåãããããç¨ãã¦ç¾å¨ã®ç¶æ ãæ´æ°ãã¾ãã
ã» ç¶æ ã®æ´æ°
ã¢ã¯ã·ã§ã³ãåãåããããã«åºã¥ãã¦ã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ ãæ´æ°ãã¾ãã
ã» ã¹ãã¢ã®ãµãã¹ã¯ãªãã·ã§ã³
ã¹ãã¢ã«å¤æ´ãå ããããéã«ããªã¹ãã¼ãéç¥ãããã¨ãã§ãã¾ãã
Reduxã®ã¹ãã¢ã¯ãåä¸ã®å ´æã«ã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ ãä¿æãããããã¢ããªã±ã¼ã·ã§ã³å ¨ä½ã§ç¶æ ã®å¤æ´ãä¸è²«ãã¦ç®¡çãããã¨ãã§ãã¾ãã
ããã«ãããã¢ããªã±ã¼ã·ã§ã³ã®è¤éããæããã¡ã³ããã³ã¹ããã¹ããç°¡åã«ãªãã¾ãã
ã¹ãã¢ã®ä½ææ¹æ³ãå¦ã³ã¾ãããã
æ¬æ¥ã¯create-react-appã使ç¨ãã¾ãã
ãã§ã«ã¤ã³ã¹ãã¼ã«ããã¦ããå ´åã¯ãä¸è¨æé ã¯ã¹ããããã¦ä¸ããã
npm install create-react-app
Reactããã¸ã§ã¯ãã®ã¢ããªãä½æãã¾ãã
npx create-react-app ä»»æããã¸ã§ã¯ãå
å¿ è¦ãªnpmããã±ã¼ã¸ã¯2ã¤ã ãã¨ãªãã¾ãã
Redux Toolkitããã³ReactReduxããã±ã¼ã¸ãä½æããReactããã¸ã§ã¯ãã«è¿½å ãã¾ãã
npm install @reduxjs/toolkit react-redux
Reduxã®ã¹ãã¢ãä½æããããã«ã¯ãcreateStore()
é¢æ°ã使ç¨ãã¾ãã
import { createStore } from 'redux'; createStore()
ãã®é¢æ°ã¯ãReduceré¢æ°ã¨åæç¶æ ãåãåããReduxã¹ãã¢ãä½æãã¾ãã
ä¸è¨ã¯ãReduxã¹ãã¢ãä½æããããã®åºæ¬çãªä¾ã¨ãªãã¾ãã
import { createStore } from 'redux'; import rootReducer from './reducers'; const initialState = { // åæç¶æ ã®è¨å® }; const store = createStore(rootReducer, initialState);
ä¸è¨ã®ä¾ã§ã¯ãcreateStore()
é¢æ°ã使ç¨ãã¦ãrootReducer
ã¨initialState
ãæå®ãã¦Reduxã¹ãã¢ãä½æãã¦ãã¾ãã
rootReducer
ã¯ãã¢ããªã±ã¼ã·ã§ã³ã®ãã¹ã¦ã®Reduceré¢æ°ãã¾ã¨ãããã®ã§ãã
initialState
ã¯ã¢ããªã±ã¼ã·ã§ã³ã®åæç¶æ
ã表ãã¾ãã
Reduceré¢æ°ãå®ç¾©ãã¦ãè¤æ°ã®Reduceré¢æ°ãã¾ã¨ããrootReducer
ãä½æãã¾ãããã
import { combineReducers } from 'redux'; import counterReducer from './counterReducer'; import todosReducer from './todosReducer'; const rootReducer = combineReducers({ counter: counterReducer, todos: todosReducer }); export default rootReducer;
ä¸è¨ã®ä¾ã§ã¯ãcombineReducers()
é¢æ°ã使ç¨ãã¦ãcounterReducer
ã¨todosReducer
ãã¾ã¨ãã¦rootReducer
ãä½æãã¦ãã¾ãã
combineReducers()
é¢æ°ã¯ãå¼æ°ã«æ¸¡ãããReduceré¢æ°ãçµåãã¦ãåä¸ã®Reduceré¢æ°ãè¿ãã¾ãã
rootReducer
ã¯ãReduxã¹ãã¢ã®ä½ææã«createStore()
é¢æ°ã«æ¸¡ããã¾ãã
ãã®ããã«ãcreateStore()
é¢æ°ã使ç¨ãããã¨ã§ãReduxã¹ãã¢ãä½æãããã¨ãã§ãã¾ãã
Reduxã¹ãã¢ã¯ãã¢ããªã±ã¼ã·ã§ã³å ¨ä½ã§ä½¿ç¨ãããã°ãã¼ãã«ãªç¶æ ã管çããããã«ä½¿ç¨ããã¾ãã
ã§ã¯ãReduxã¹ãã¢å ã«ããç¾å¨ã®ã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ ãåå¾ããä¾ã示ãã¾ãã
ãããè¡ãã«ã¯ãReduxã®getState()
ã¡ã½ããã使ç¨ãã¾ãã
Reduxã¢ããªã±ã¼ã·ã§ã³ã§ã¯ãã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ ã¯åä¸ã®ã¹ãã¢ã«éç´ããã¦ãã¾ãã
getState()
ã¡ã½ããã使ç¨ãããã¨ã§ãReduxã¹ãã¢å
ã®ç¾å¨ã®ç¶æ
ãåå¾ãããã¨ãã§ãã¾ãã
import store from './store'; const currentState = store.getState();
ä¸è¨ã®ä¾ã§ã¯ãReduxã¹ãã¢å
ã®ç¾å¨ã®ç¶æ
ãcurrentState
å¤æ°ã«ä»£å
¥ãã¦ãã¾ãã
ãã®ç¶æ ãªãã¸ã§ã¯ãã¯ãReduxã¹ãã¢å ã®ç¾å¨ã®ã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ ã表ãã¦ãã¾ãã
ãã®ããã«ãgetState()
ã¡ã½ããã使ç¨ãããã¨ã§ãReduxã¹ãã¢å
ã®ç¾å¨ã®ç¶æ
ãç°¡åã«åå¾ãããã¨ãã§ãã¾ãã
注æç¹ã¯ãgetState()
ã¡ã½ããã¯ãReduxã¹ãã¢å
ã®ç¶æ
ãç´æ¥å¤æ´ãããã¨ã¯ã§ãã¾ããã
ç¶æ
ãå¤æ´ããããã«ã¯ãdispatch()
ã¡ã½ããã使ç¨ããå¿
è¦ãããã¾ãã
ç¶æ ãdispatch(action)ãä»ãã¦æ´æ°å¯è½ã«ããæ¹æ³ããããããã¦ã¿ã¾ãããã
Reduxã®dispatch()
ã¡ã½ããã¯ãã¢ããªã±ã¼ã·ã§ã³å
ã§ã¢ã¯ã·ã§ã³ãçºçãããReduxã¹ãã¢ã«å¯¾ãã¦ãã®ã¢ã¯ã·ã§ã³ãéä¿¡ããããã®ã¡ã½ããã§ãã
Reduxã§ã¯ãã¢ããªã±ã¼ã·ã§ã³å
ã§ä½ããã®ã¤ãã³ããçºçããå ´åãããã表ãã¢ã¯ã·ã§ã³ãªãã¸ã§ã¯ããä½æããdispatch()
ã¡ã½ãããå¼ã³åºãã¦Reduxã¹ãã¢ã«éä¿¡ãã¾ãã
Reduxã¹ãã¢ã¯ããã®ã¢ã¯ã·ã§ã³ã«åºã¥ãã¦ç¾å¨ã®ç¶æ ãæ´æ°ããæ´æ°ãããç¶æ ãå ¨ã¦ã®ã³ã³ãã¼ãã³ãã«éç¥ãã¾ãã
ããã¯ãã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ ãæ´æ°ããå¯ä¸ã®æ¹æ³ã¨ãªãã¾ãã
import store from './store'; store.dispatch({ type: 'ADD_TODO', payload: { text: 'Learn Redux', completed: false } });
ä¸è¨ã®ä¾ã§ã¯ãdispatch()
ã¡ã½ããã使ç¨ãã¦ãã¢ããªã±ã¼ã·ã§ã³å
ã§ã®ãæ°ããã¿ã¹ã¯ã追å ãããã¨ããã¢ã¯ã·ã§ã³ãReduxã¹ãã¢ã«éä¿¡ãã¦ãã¾ãã
ã¢ã¯ã·ã§ã³ã¯ãtype
ã¨payload
ã¨ãã2ã¤ã®ããããã£ãæã¤ãªãã¸ã§ã¯ãã¨ãã¦å®ç¾©ããã¦ãã¾ãã
dispatch()
ã¡ã½ããã¯ãã¢ã¯ã·ã§ã³ãªãã¸ã§ã¯ããReduxã¹ãã¢ã«éä¿¡ããã ãã§ãªãããã®ã¢ã¯ã·ã§ã³ãå¦çããããã®Reduceré¢æ°ãå¼ã³åºãã¾ãã
ãã®Reduceré¢æ°ã¯ãã¢ã¯ã·ã§ã³ã«åºã¥ãã¦Reduxã¹ãã¢ã®ç¶æ ãæ´æ°ãã¾ãã
subscribe(listener)ãä»ãã¦ãªã¹ãã¼ãç»é²ããä¾ã示ãã¾ãã
Reduxã®subscribe()
ã¡ã½ããã使ç¨ããã¨ãReduxã¹ãã¢å
ã®ç¶æ
ã®å¤æ´ãç£è¦ããããã®ãªã¹ãã¼ãç»é²ãããã¨ãã§ãã¾ããã
import store from './store'; const listener = () => { console.log('Redux state updated:', store.getState()); }; store.subscribe(listener);
ä¸è¨ã®ä¾ã§ã¯ãsubscribe()
ã¡ã½ããã使ç¨ãã¦listener
é¢æ°ãReduxã¹ãã¢ã«ç»é²ãã¦ãã¾ãã
listener
é¢æ°ã¯ãReduxã¹ãã¢ã®ç¶æ
ãæ´æ°ãããã¨ãã«å¼ã³åºããã¾ãã
store.subscribe()
ã¡ã½ããã¯ãç»é²ããããªã¹ãã¼é¢æ°ãè¿ãã¾ãã
å¿ è¦ã«å¿ãã¦ãå¾ã§ãªã¹ãã¼ã解é¤ãããã¨ãã§ãã¾ãã
ä¸è¨ã®ä¾ã§ã¯ããªã¹ãã¼é¢æ°ãReduxã¹ãã¢ã®ç¶æ ãã³ã³ã½ã¼ã«ã«ãã°åºåããã ãã§ãããå®éã®ã¢ããªã±ã¼ã·ã§ã³ã§ã¯ããªã¹ãã¼é¢æ°ã使ç¨ãã¦Reduxã¹ãã¢ã®ç¶æ ãæ´æ°ãããããªå¦çãå®è¡ãããã¨ãã§ãã¾ãã
ããã§ã¯ããªã¹ãã¼ã®ç»é²è§£é¤ã®å¦çãè¡ãæ¹æ³ã§ãã
Reduxã®subscribe()
ã¡ã½ããã使ç¨ãã¦ç»é²ããããªã¹ãã¼ã解é¤ããã«ã¯ãsubscribe()
ã¡ã½ãããè¿ãé¢æ°ãå¼ã³åºãã¾ãã
ãã®é¢æ°ãå¼ã³åºããã¨ã§ãç»é²ãããªã¹ãã¼ãReduxã¹ãã¢ããåé¤ããã¾ãã
const listener = () => { console.log('Redux state updated:', store.getState()); }; const unsubscribe = store.subscribe(listener) unsubscribe() //ãªã¹ãã¼ã®è§£é¤
unsubscribe()
ã¯ãç¶æ
ãå¤åããã¨ãã«ãªã¹ãã¼ã¡ã½ãããå¼ã³åºããããªãå ´åã«ãå½¹ç«ã¡ã¾ãã
Reduxã¹ãã¢ã®ç¶æ ãå¤æ´ãããã¨ãã«ããã®å¤æ´ãç£è¦ããããã®ãªã¹ãã¼ãç°¡åã«ç»é²ããã³è§£é¤ãããã¨ãã§ãã¾ãã
ãã ããéå°ãªä½¿ç¨ã¯ããã©ã¼ãã³ã¹ã«å½±é¿ãä¸ããå ´åãããã®ã§ãé©åãªã¿ã¤ãã³ã°ã§ãªã¹ãã¼ãç»é²ããå¿ è¦ãããã¾ãã®ã§ã注æä¸ããã
Reduxã¯ã¾ã æ£ããé¸æãªã®ãï¼
ã¾ããReduxã¯ãã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ 管çã®ããã«è¨è¨ãããã©ã¤ãã©ãªã§ããããã¼ã¿ãä¿åããããã«ã¯é©ãã¦ãã¾ããã
Reduxã¯ãã¢ããªã±ã¼ã·ã§ã³å ã®ã³ã³ãã¼ãã³ãéã§å ±æãããç¶æ ã管çããããã«ä½¿ç¨ããã¾ãã
ãã®ç¶æ ã¯ãã¦ã¼ã¶ã¼ãã¢ããªã±ã¼ã·ã§ã³ã§å®è¡ããæä½ã«ãã£ã¦å¤æ´ãããReduxã«ãã£ã¦ç®¡çããã¾ãã
ä¸æ¹ã§ããã¼ã¿ã®ä¿åã«ã¯ãé常ã¯æ°¸ç¶çãªãã¼ã¿ã¹ãã¬ã¼ã¸ãå¿ è¦ã§ãã
Reduxã¯ãä¸æçãªã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ 管çã«é©ãã¦ãã¾ãããæ°¸ç¶çãªãã¼ã¿ä¿åã«ã¯é©ãã¦ãã¾ããã
æ°¸ç¶çãªãã¼ã¿ä¿åãå¿ è¦ãªå ´åã¯ã代ããã«ãã¼ã¿ãã¼ã¹ããã¡ã¤ã«ã·ã¹ãã ãªã©ã®æ¹æ³ã使ç¨ããå¿ è¦ãããã¾ãã
ã§ããããã¼ã¿ç®¡çã¯é常ã«å°é£ã§ãã
ã¢ããªã±ã¼ã·ã§ã³ãé²åããã«ã¤ãã¦ããã¼ã¿ã管çããããã®æ¦ç¥ãç«ã¦ããã¨ãä¸å¯æ¬ ã¨ãªãã¾ãã
使ç¨ããç¶æ 管çã©ã¤ãã©ãªãä½ã§ãããé©åãã¤å¾¹åºçã«è¨è¨ãããã¢ããªã±ã¼ã·ã§ã³ã¯ãéçºè ã®çç£æ§ã«å¤§ããªéããããããå¯è½æ§ãããã¾ãã
Reactã¨æ°ããã©ã¤ãã©ãªã®ãããã§ãReduxã¯ãã¯ãå¯ä¸ã®é¸æè¢ã§ã¯ãªããªã£ã¦ãã¦ãã¾ãã
ãã¼ã¿ã¨ã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ ã管çããæ¹æ³ãç°¡ç´ åããããã«ãä»ã®æ°ããã©ã¤ãã©ãªã¨ãã¯ããã¸ã¼ãå©ç¨å¯è½ã§ãã
Reduxã¯ããã¼ã¿ãã³ã³ãã¼ãã³ãã«æ¸¡ãåé¡ã解決ããã®ã§ããã¸ã§ã¯ãã«çµã¿è¾¼ã¾ãããã¨ãããããã¾ãã
ãããã種é¡ã®ãã¼ã¿ã«ã©ãããã§ãã¢ã¯ã»ã¹ã§ããããã«ãªãã¾ãã
ã§ãããéçºè ãããããã¡ãªééãã®1ã¤ã«Reduxã使ãããããã¨ã§ãã
管çãé常ã«å°é£ãªå¤§è¦æ¨¡ãªãã¼ã¿ã¹ãã¢ã«ç´é¢ããæã«ãåæã«ãã£ã¹ããããããã¢ã¯ã·ã§ã³ãå¤ããã¦ã¢ããªã±ã¼ã·ã§ã³ã®ããã©ã¼ãã³ã¹ãä½ä¸ããã¹ãã¢ãæ´æ°ããåã¬ã³ããªã³ã°ãå¢ãããã¨ãããã¾ãã
ã¤ã¾ããã¢ããªã±ã¼ã·ã§ã³å ã®ãã¹ã¦ã®ç¶æ ãåä¸ã®ã¹ãã¢ã«æ ¼ç´ããããããç¶æ ãæ´æ°ããããã³ã«ã¢ããªã±ã¼ã·ã§ã³å ¨ä½ãåã¬ã³ããªã³ã°ãããå¯è½æ§ãããã¾ãã
ããã¯ãã¢ããªã±ã¼ã·ã§ã³ã®ããã©ã¼ãã³ã¹ãä½ä¸ãããå¯è½æ§ãããã¾ãã
ã¾ããè¤éãªã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ 管çãæ å½ããããã«å¤æ°ã®æ¦å¿µãã¢ã¼ããã¯ãã£ãAPIãæä¾ãã¾ãã
ã¢ããªã±ã¼ã·ã§ã³ãé常ã«è¤éã§ããå ´åãReduxã«ãã£ã¦ç®¡çãããã³ã¼ãããããã°ãããã¨ãããå°é£ã«ãªããã³ã¼ããè¤éåãã¦ããå¯è½æ§ãããã¾ãã
ããããªããããã®éè¦æ§ã¯å¦å®ã§ããGithubããã®æéãã¦ã³ãã¼ãæ°ã¯3200ä¸ãè¶ ãã¦ããã¾ãã
ã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ ã¨ãã¸ãã¯ãä¸å åãããã¨ã«ããã(å ã«æ»ã/ããç´ã)ãç¶æ ã®æ°¸ç¶åãªã©ã®å¼·åãªç¶æ 管çæ©è½ãæä¾ãã¾ãã
æå¾ã«
Reduxã¯ãã°ãã¼ãã«ãªç¶æ 管çãå¿ è¦ã¨ããå°è¦æ¨¡ããã³å¤§è¦æ¨¡ãªã¢ããªã±ã¼ã·ã§ã³ãæ§ç¯ããããã®åªããå¤æ°ã®ãã¼ã«ãã©ã¤ãã©ãªãæä¾ãã¾ãã
ããã«ãã£ã¦ãéçºè ã¯ç¶æ 管çã«é¢ããéè¦ãªåé¡ã«å¯¾å¦ããããã«æéãç¯ç´ã§ããçç£æ§ãåä¸ãã¾ãã
åç´ãªã±ã¼ã¹ã§ã¯ãMobXãPushStateãReact Contextãªã©ã®è»½éãã¼ã«ããããã¾ãã
Contextã¯ä¸è¨ã§å¦ç¿ããäºãã§ãã¾ãã
ã¨ã³ã¸ãã¢ã®è¦³ç¹ããè¦ãã¨ãReduxã®å¤§ããªã¡ãªããã¯ãéçºè ã®ã¨ã¯ã¹ããªã¨ã³ã¹ãåä¸ããããã¨ã§ãã
è¤éãªãã¸ãã¯ãã¢ããªå ¨ä½ã«åæ£ãããã®ã§ã¯ãªãæ´çãããã¨ã§ãè¤éãªãã¸ãã¯ã®å¦çã容æã«ãªãã¾ãã
ãã®ä¸ãã¢ããªã±ã¼ã·ã§ã³å ã®ç¶æ ãå¤æ´ããããã«å³å¯ãªã«ã¼ã«ã¨å¶ç´ãè¨ãããã¨ã§ãç¶æ ã®å¤æ´ãäºæ¸¬å¯è½ã«ãã¾ãã
ããã«ãããéçºè ã¯æå³ããã«ç¶æ ãå¤æ´ãããã¨ãé²ãããããã°ããã¹ãã容æã«ãªãã¾ããã
ããã¦ãç¶æ 管çã¨ãã¸ãã¹ãã¸ãã¯ã®ã³ã¼ããåé¢ããããã®ã¢ã¼ããã¯ãã£ãæä¾ãã¾ãã
ã¢ããªã±ã¼ã·ã§ã³ã®ã³ã¼ããããã¢ã¸ã¥ã¼ã«åãããå¯èªæ§ãåä¸è´ãã¾ãã
ä¸è¨ã®ãããªãã¡ãªããããããããReduxã¯å¤ãã®Reactéçºè ã«ã¨ã£ã¦äººæ°ã®ããã©ã¤ãã©ãªã¨ãªã£ã¦ãã¾ãã
æ¬æ¥ã¯ä»¥ä¸ã¨ãªãã¾ãã
æå¾ã¾ã§èªãã§é ããããã¨ããããã¾ãã
ãã®è¨äºãæ°ã«å ¥ã£ãããããã¯ãã¼ã¯ãä»ã®æ¹ã«ãå ±æãã¦ãã ããã