rubies rubies - 1 year ago 107
React JSX Question

Array inside an Object with Dynamic Key in Redux

Given I have a an array.

var myKey = "myObjectKey";
var anArray = ["apple", "balloon", "dog", "cat"]; {
store.dispatch(Object.assign({type: 'ADD_ITEM', payload: thing, key: myKey}));

How do I get the following output?

# Object: {myObjectKey: ["apple", "balloon", "dog", "cat"]}

Answer Source

This should work. Just pass the object to the dispatch. It will be handelled in the reducer

var myKey = "myObjectKey";
var anArray = ["apple", "balloon", "dog", "cat"];

  store.dispatch({type: 'ADD_ITEM', payload: { [myKey]: anArray} })
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download