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

Teach useLiferayState 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:

      Obvious counterpart to LPS-131052 (see that one for more context), the proposal here is to accept the "functional updater" pattern that is familiar from React's useState hook (docs) in our own useLiferayState hook:

      const [counter, setCounter] = useLiferayState(atom);
      
      // This is the functional update:
      setCounter((previous) => previous + 1);
      
      // But note that the straight-up value-based update still works:
      setCounter(0);

      The usual gotchas about stale closures apply. In practice if you have a counter-like value in state, the functional updater pattern is one you will want, unless you're doing a reset back to a fixed value like I am in the example.

        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