Redux Toolkitã®createSliceã¯ã¡ãã£ã¨Reduxã½ããªãå¦çããã¦ãããã®èæ¯ã«ããå²å¦ã¯ä½ãã
ç¹å¾´: Actionã¨Reducerã®åé/å¯çµå
Reduxã¯ãActionã¨Reducerã¯N:M対å¿ãäºãã«ççµåãã¨ããå²å¦1,2.
ãããRTK.createSliceã¯CaseReducerããAction/ActionCreatorãèªåçæããã
ã¤ã¾ããActionã¨Reducerã¯1:1対å¿ãã«ãã¦ãã¾ã.
ããã¯ã±ã£ã¨è¦ãReduxã®å²å¦ã«åãã¦ããï¼ããæãã¦ãã人ã®ã³ã¡ã³ã link1, 2, 3ï¼.
åé¡æè: æ©è½çåéãççµåã«ãããª
Reduxãæ¡ç¨ããè¦æ¨¡ã®ç¶æ
管çã§ã¯ã2種é¡ã®Actionãç¾ãã.
(ããã§ã®å½åã¯ç§)
- cross-domain Action: è¤æ°ã®domain3ã«ã¾ãããAction
- Action:Reducer = N:MãEventãªã¤ã¡ã¼ã¸
- in-domain Action: 1ã¤ã®domainã«ã®ã¿å½±é¿4
- Action:Reducer = 1:1ãRPCãªã¤ã¡ã¼ã¸
- Actionè¨è¨æ®µéããå¦çï¼reducerï¼ã«1:1ã§çµã³ã¤ãã¦ãã
Reduxå²å¦ã¨in-domain Actionã®é¢ä¿ãåé¡ã
Actionã¨Recuder (ã¤ãã³ãã¨å¦ç) ãè¨è¨ããçµã³ã¤ãã¦ãã == æ©è½çã«åéãã¦ããã
ãªã®ã§åéãã¦ããã®ãRedux-wayã§ããããççµåã«ããã®ã¯åé度ãä¸ãã¡ããéå°ãªæ½è±¡åãKISSååã®éåã
Ducksãã¿ã¼ã³ã¯ããã«å¯¾ããåçã®1ã¤5ã
èæ ®ç¹: cross-domainãã£ã¦ã®Redux
å
¨é¨ãin-domain ActionãªãReduxãããªãã
ã§ãããç¨åº¦ã®è¦æ¨¡ã«ãªãã¨cross-domainãæ··å¨ãã¦ãã.
in-domain Actionãcross-domain Actionã¨ãã¦å©ç¨ãããæ¥ãåºã¦ãã.
ãã®æ··å¨ãã©ããããèæ
®ãå¿
è¦.
解決ç: in-domainã¯åéããã¦cross-domainã¯ççµå
Redux Toolkitã®createSliceã¯ä¸¡ã¿ã¤ãã®Action/Reducerãæ±ã.
- in-domain
- CaseReducerã«åºã¥ããAction/ActionCreatorçæ 6ï¼æ©è½çåéï¼
- cross-domain
Actionã¨ããã¡ãã»ã¼ã¸ããã·ã³ã°ã®æ çµã¿ã¯ä¿æï¼== Reduxï¼ã
ãã®ä¸ã§in-domainãªå¦çã¯Actionèªåçæ&Reducerã¨åãä½ç½®ã«ä¿æï¼Ducksã©ã¤ã¯8, 9, 10ï¼ã«ãããã¨ã§Actionãæèãããªãä½ãã«ãã.
cross-domainã¯ä»ã¾ã§ã©ãã使ããããããèªåçæã®Actionãcross-domainã¸è»¢ç¨ã§ãããããªä½ã (1:1ã«è¦ãããã©1:Nã«ä½¿ãã).
é¨åé¨åã§å¿
è¦ãªæ½è±¡åº¦ãå¶å¾¡ãã¦KISSååãå®ããusefulnessãä¸ããï¼ãéå°ãªæ½è±¡åã«ãã大éã®ãã¤ã©ã¼ãã¬ã¼ããã®é¤å»ï¼ã
Refs
- reduxjs/redux-toolkit - Feature discussion:
createSlice
behavior #91- selectorã®è°è«ãã¡ã¤ã³
- reduxjs/redux-toolkit - Discussion: Roadmap to 1.0 #82
-
‘One of the key concepts of Redux is that each slice reducer “owns” its slice of state, and that many slice reducers can independently respond to the same action type.’ Redux Toolkit↩
-
“caveats to keep in mind: Actions are not exclusively limited to a single slice. Any part of the reducer logic can (and should!) respond to any dispatched action.” Redux Toolkit↩
-
== slice == FluxStore ↩
-
“95% of the time, it’s only one reducer/actions pair that ever needs their associated actions.” Ducks ↩
-
“for these pieces to be bundled together in an isolated module that is self contained” ↩
-
“the point of createSlice is … automatically generate the actions and types so that they don’t have to.” Redux Toolkit issue ↩
-
“I do want createSlice to support … that includes listening for action types that aren’t defined by this slice.” Redux Toolkit issue ↩
-
‘the result object is conceptually similar to a “Redux duck” code structure.’ Redux Toolkit ↩
-
‘createSlice does work as a “ducks” generator just fine.’ Redux Toolkit issue ↩
-
‘to some extent createSlice does edge kinda close to the “Redux modules” concept’ Redux Toolkit issue ↩