Would it be a good/bad idea to develop Lightning Components as “Pure Components”?

As a developer, I enjoy playing with different technologies and seeing what I can learn from, and what ideas I might bring back when working on SFDC (my bread and butter).

I’m currently looking at ReactJS and learning about “Pure Components” and the idea that most components should be stateless which allows for a more functional style of development and testing.

Obviously, since SFDC isn’t giving us immutable collections or variables or even a way to annotate Lightning components as pure, there will be limits to how well we might leverage the idea in SFDC and what benefits we might see.

But I’m wondering if anyone has had any success (or noteworthy failures) trying to develop Lightning Components as pure components and whether you’d have any suggestions for anyone else who might want to go down that road?

Answer

The primary purpose of React’s pure components is to be able to determine if it needs to rerender a component based on the values passed in. Aura does not have this level of sophistication, instead rerendering based on aura:valueChange events; in the truest sense, a pure component in Lightning would necessarily be static. That said, any component that doesn’t expose a UI, like a data service component, could be written as a pure component.

On the other hand, calls to the server can work like pure functions (via setStorable). If you can ascertain that a particular call is idempotent within a reasonable amount of time, it might make sense to make your actions storable, which allows Aura to cache response values based on the parameters passed in to the action, avoiding additional round trips to the server when they aren’t necessary.

Note that while the responses are cached, they are automatically updated periodically, which can result in the same callback being called more than once. You may need to account for this. You can read more about it in Storable Actions.

Attribution
Source : Link , Question Author : Brian Kessler , Answer Author : sfdcfox

Leave a Comment