[SalesForce] Unique Identifier of a Lightning Component Instance

I'm working on a relatively simple Aura component, but I'm running into an issue. I'd like to create either a cookie or a localStorage item for a component when a user takes action on it (closes it) so that the instance of the component 'remembers' if it should be closed when the page is reloaded. Otherwise, the component will simply reappear when the page reloads.

The issue is that I'm unsure if there is a way to either find or assign a globally unique identifier for an instance of a component. There could be multiple instances of this same component on a page, and it can be used on any flexipage, so something like a record Id wouldn't be applicable.

I've tried setting a randomly generated ID attribute on init, but I've not determined a way for this id to persist on the component. Whenever it loads, that ID simply gets regenerated.

TL;DR I need a way to find or assign a globally unique identifier to each instance of a component that is persistent, if one even exists

Best Answer

Strictly speaking, no, it is not possible. Depending on if you're talking about components that will be bound to a page, or used within other components, you'll generally want to have an attribute that a developer or designer can set that will uniquely identify the component by a given name, then you can use localStorage/sessionStorage/cookies/etc to remember that component instance. Alternatively, you might devise a way to come up with a unique hash based on the component's inputs to calculate a more-or-less universally unique identifier.

Related Topic