1

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

1

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.

| improve this answer | |
  • This is what I'm going to do, after all. The system I'm building is going to be tweaked to allow for a record Id to be referenced in the component's design attributes, and that will then serve as the unique identifier of that component for use ina cookie or localStorage item. – Luke Miller Jun 4 at 22:43
1

Check out the following link:

https://medium.com/@strusov/working-with-cookies-from-lightning-79073b2649f9

It discusses a potential cookie-based approach that might help you.

| improve this answer | |
  • The cookie functions are great, and are likely what I would use to 'remember' the action taken. However, I still run into the issue that there is no way for this single instance of a component to be identified by a cookie. If multiple components are used on a page, they would all be affected by one being closed. – Luke Miller Jun 2 at 22:21
  • It might help if you expanded on the use case by providing an example. I understand the problem you're trying to overcome but don't really understand why. Why do you need to use a single component several times on the same page in a way that implies the component has more than one purpose, or the purpose(s) isn't data-driven? I'm definitely intrigued, if not also completely confused. – Basher Brain Jun 2 at 22:56
0

It looks like component.getGlobalId() should do the trick. See the official Component Id options here and their examples below:

HTML

<div id="{!globalId + '_footer'}"></div>

Controller Code

var globalId = cmp.getGlobalId();
| improve this answer | |
  • 1
    This is what I had hoped was the answer initially, however this line in the documentation points to why it wouldn't work as a permanent identifier: "A global ID (1) is not guaranteed to be the same beyond the lifetime of a component, so it should never be relied on." When the page is refreshed, the Id is likely to update to another value, meaning any cookie or localStorage item would no longer correctly indicate that instance of the component. – Luke Miller Jun 2 at 22:01
  • Ah got it, I hear you. Curious if there's any way you can reengineer things so that you don't run into this situation? Or update your question to give a little more context on what you're trying to achieve in a broader sense? – Brian Miller Jun 2 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.