salesforce / lwc

⚡️ LWC - A Blazing Fast, Enterprise-Grade Web Components Foundation
https://lwc.dev
Other
1.62k stars 394 forks source link

Supporting User Created Decorators (or Cross-Cutting Concerns) #1332

Open larkintuckerllc opened 5 years ago

larkintuckerllc commented 5 years ago

I am trying to figure out how to implement cross-cutting concerns in LWC and thought to use decorators; LWC is class-based and this one such way of doing this.

But, when I went implement my own decorator, I get the following error message.

"Invalid decorator usage. Supported decorators (api, wire, track) should be imported from "lwc"

I either would like to see a good example of implementing a cross-cutting concern, like HOC, RenderProps, or Hooks in React. Again, I thought decorators were the answer; so enabling them more generally might be the answer?

caridy commented 5 years ago

@larkintuckerllc this is a great question. Due to the uncertainty of the standardization of decorators, we have decided to restrict the decorators allowed by our LWC compiler until we are certain about the future of decorators. At the moment, I don't see a way to manipulate the build step to have your own decorators.

larkintuckerllc commented 5 years ago

Coincidentally, I just bumped into an interesting JavaScript pattern that potentially provides a pattern for cross-cutting concerns with LWC. The beauty of the pattern is that once you see it, it is super simple and obvious. http://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/

Looking at using this pattern with some real-world examples, e.g., redux and apollo client

caridy commented 5 years ago

yes @larkintuckerllc that works fine with LWC, in fact we have some folks using it, but, we do not recommend it if you are planning to share your component thru salesforce platform. Until we can land the syntax in Javascript (we are working with google folks on that), we have no way to do a full static analysis of the component, and therefore, part of the referencial integrity aspect of the platform cannot be preserved for those components.

larkintuckerllc commented 5 years ago

FYI... Through an effort to incorporate Apollo Client, ended up learning about an officially supported pattern for cross-cutting concerns in LWC; just not well documented. Here is an article on the topic:

https://medium.com/@johntucker_48673/cross-cutting-concerns-in-lightning-web-components-solved-e8eca2c4ad3?source=friends_link&sk=55ebcbb04d54e0d52c91ddccff474661