Uploaded image for project: 'PUBLIC - Liferay Portal Community Edition'
  1. PUBLIC - Liferay Portal Community Edition
  2. LPS-131052

Teach Liferay.State.writeAtom to allow functional updater pattern

    Details

    • Type: Task
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: JS APIs and Utilities
    • Labels:
      None

      Description

      WISHLIST ITEM — We don't yet have an immediate need for this, but it is an obvious potential improvement, so I want to make a note of it.

      Sibling issue to:

      When working on LPS-130359 I found myself in the original draft PR wanting to be able to read from an atom only to write straight back to it. In the final version of the PR, I refactored to do things in a different way, but here is the example of what I was wanting to do originally:

      const value = Liferay.State.readAtom(atom);
      
      const transformedValue = mush(value);
      
      Liferay.State.writeAtom(atom, transformedValue);
      

      So the proposal here is to accept the "functional updater" pattern that is familiar from React's useState hook (docs):

      // This is the functional update:
      Liferay.State.writeAtom(atom, (previous) => mush(value));
      
      // But note that the straight-up value-based update still works:
      Liferay.State.writeAtom(atom, someValue);

      An obvious corollary here would be to update useLiferayState to also allow this kind up write, but I'll create a separate ticket for that (LPS-131055).

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              support-lep@liferay.com SE Support
              Reporter:
              greg.hurrell Greg Hurrell (Inactive)
              Recent user:
              Greg Hurrell (Inactive)
              Participants of an Issue:
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:

                  Packages

                  Version Package