ํ์ฌ bindActionCreators
๋ ๋จ์ผ ํจ์ ๋๋ ํจ์ ๊ฐ์ฒด๋ฅผ ํ์ฉํฉ๋๋ค. ์ด ์ฝ๋ ํจํด์ ๋๊ธฐ ์ํด ๋ฐฐ์ด๋ ํ์ฉํ๋ฉด ์ ์ฉํ ๊ฒ์
๋๋ค.
import * as ac from './actionCreators';
// later in file...
const [doThis, doSomethingElse, doAnotherThing] = bindActionCreators(
[ac.doThis, ac.doSomethingElse, ac.doAnotherThing
], dispatch);
// ...
๋ชจ๋ ์ค์๋ eslint-plugin-import/namespace ์ ์ํด ์ค๋ฅ๋ก ์ฆ์ ํฌ์ฐฉ๋๊ธฐ ๋๋ฌธ์ ์์ฑ์ ์ด๋ฆ์ ๋ช ์์ ์ผ๋ก ์ง์ ํ๋ ๊ฒ์ ์ข์ํฉ๋๋ค.
๋๋ ์ด๊ฒ์ด ํ์ํ ๋ณํ๋ผ๊ณ ์๊ฐํฉ๋๋ค.
diff --git a/src/bindActionCreators.js b/src/bindActionCreators.js
index e70e270..6050c32 100644
--- a/src/bindActionCreators.js
+++ b/src/bindActionCreators.js
@@ -28,6 +28,10 @@ export default function bindActionCreators(actionCreators, dispatch) {
return bindActionCreator(actionCreators, dispatch)
}
+ if (Array.isArray(actionCreators)) {
+ return actionCreators.map(ac => bindActionCreator(ac, dispatch));
+ }
+
if (typeof actionCreators !== 'object' || actionCreators === null) {
throw new Error(
`bindActionCreators expected an object or a function, instead received ${actionCreators === null ? 'null' : typeof actionCreators}. ` +
bindActionCreator
๋ฅผ ๋ ๋คํ์ฑ์ผ๋ก ๋ง๋๋ ๊ฒ์ ๋๋ฌด ๋ง์ ์ผ์ด๋ผ๊ณ ์๊ฐํฉ๋๋ค. ์ด ์์
์ ์ํํ๋ ๋ํ ํจ์๋ฅผ ๋ง๋๋ ๊ฒ์ ์ด๋ป์ต๋๊น?
function mapBindActionCreators(actions, dispatch) {
return actions.map(action => bindActionCreator(action, dispatch)
}
๋ฐฐ์ด์ด ์๋ ๊ฒฝ์ฐ reduce()
๋ฐฐ์ด์ ๊ฐ์ฒด์ ๋ฃ๊ณ bindActionCreator
์ ์ ๋ฌํ ์ ์์ต๋๋ค.
์, ์ฌ๊ธฐ์์ ๊ฐ๋ฅํ ๋ชจ๋ ์์ด์ ์ง์ํ์ง ์๋ ๊ฒ์ด ์ข์ต๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
bindActionCreator
๋ฅผ ๋ ๋คํ์ฑ์ผ๋ก ๋ง๋๋ ๊ฒ์ ๋๋ฌด ๋ง์ ์ผ์ด๋ผ๊ณ ์๊ฐํฉ๋๋ค. ์ด ์์ ์ ์ํํ๋ ๋ํ ํจ์๋ฅผ ๋ง๋๋ ๊ฒ์ ์ด๋ป์ต๋๊น?