Skip to content

Prefixes/Namespacing proposed syntax #159

@Strandedpirate

Description

@Strandedpirate

For namespacing, which I would like to see, how about supporting this kind of syntax? This actually reduces boilerplate in the destructing assignment for all 2'nd level action creators and prevents having to keep keys and their destructed names in sync.

const {
  rootLevelAction,
   // no keys below products or orders must be kept in sync here. yeah!!!
  products,
  orders,
} = createActions({
	rootLevelAction: () => ({}),
	products: { // this key used for the namespace prefix
	  list: (query) => ({ query }),
	  addToCart: (quantity, productId) => ({ quantity, productId }),
	},
	orders: { // this key used for the namespace prefix
	  placeOrder: (cartId) => ({ cartId }),
	  getOrder: (orderId) => ({ orderId }),
	}
});

Example usage:

dispatch(rootLevelAction());
dispatch(products.list('Star Wars'));
dispatch(products.addToCart(1, 45243));
dispatch(orders.placeOrder(7348));
dispatch(orders.getOrder(45));

Destructures to the following:

rootLevelAction: function() {
	return {
		type: 'rootLevelAction',
		payload: { }
	};
},
products:{
	type: 'products',
	list: function(query) {
		return {
			type: 'products/list', // boom, namespaced to reduce collisions
			payload: { query }
		};
	},
	addToCart: function(quantity, productId) {
		return {
			type: 'products/addToCart',
			payload: { quantity, productId }
		};
	}
},
orders:{
	type: 'orders',
	placeOrder: function(cartId) {
		return {
			type: 'orders/placeOrder',
			payload: { cartId }
		};
	},
	getOrder: function(orderId) {
		return {
			type: 'orders/getOrder',
			payload: { orderId }
		};
	}
}

Supporting an unlimited level of literal nesting would be awesome and allow for richly namespaced action creators that reduce the chance of collisions.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions