Redux: Funktionsanforderung: bindActionCreators erlauben, ein Array zu akzeptieren

Erstellt am 10. Juni 2016  ·  3Kommentare  ·  Quelle: reduxjs/redux

Derzeit akzeptiert bindActionCreators eine einzelne Funktion oder ein Objekt von Funktionen. Es wäre nützlich, wenn es auch ein Array akzeptieren würde, um dieses Codemuster zu unterstützen:

import * as ac from './actionCreators';

// later in file...
const [doThis, doSomethingElse, doAnotherThing] = bindActionCreators(
  [ac.doThis, ac.doSomethingElse, ac.doAnotherThing
], dispatch);
// ...

Ich gebe meine Erstellernamen gerne explizit an, da Fehler sofort von eslint-plugin-import/namespace als Fehler abgefangen werden.

Ich glaube, das ist die notwendige Änderung:

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}. ` +

Hilfreichster Kommentar

Ich denke, es wäre zu viel, bindActionCreator polymorpher zu machen. Wie wäre es, wenn Sie einfach eine Wrapping-Funktion erstellen, die dies für Sie erledigt?

function mapBindActionCreators(actions, dispatch) {
  return actions.map(action => bindActionCreator(action, dispatch)
}

Alle 3 Kommentare

Ich denke, es wäre zu viel, bindActionCreator polymorpher zu machen. Wie wäre es, wenn Sie einfach eine Wrapping-Funktion erstellen, die dies für Sie erledigt?

function mapBindActionCreators(actions, dispatch) {
  return actions.map(action => bindActionCreator(action, dispatch)
}

Wenn Sie ein Array davon haben, können Sie das Array reduce() in ein Objekt einfügen und dieses an bindActionCreator übergeben

Ja, ich denke, wir möchten vermeiden, hier jede mögliche Permutation zu unterstützen.

War diese Seite hilfreich?
0 / 5 - 0 Bewertungen

Verwandte Themen

olalonde picture olalonde  ·  3Kommentare

ramakay picture ramakay  ·  3Kommentare

rui-ktei picture rui-ktei  ·  3Kommentare

CellOcean picture CellOcean  ·  3Kommentare

mickeyreiss-visor picture mickeyreiss-visor  ·  3Kommentare