Visual Editing

useOptimistic

Instantly update UI with mutation results created directly in an application whilst awaiting updated content from Content Lake.

The useOptimistic hook uses a local document store to enable developers to opt-in to instant updates for specific content. UI can be updated with the anticipated result of a mutation, avoiding the delay required when submitting and refetching data from Content Lake.

It's primarily used for enabling drag and drop functionality for Visual Editing. You can learn more about how to use it in the Drag and drop documentation.

useOptimistic

  • useOptimistic(passthrough, reducer):

    Returns

    When no mutations are pending, the hook returns the original passthrough value.

    When mutations are pending, it returns the optimistically updated state as determined by the reducers.

    In production, the useOptimistic is a no-op and will always return the passthrough value.

    Parameters

    • passthrough

      T

      The base state that should be modified optimistically. Most often the array of items that you want to make drag and droppable in Presentation.

    • reducer

      OptimisticReducer<T,U>|Array<OptimisticReducer<T,U>>

      A reducer function or array of reducers that determine how the state should be updated in response to optimistic mutations.

  • Reducers(state, action):

    A reducer function or array of reducers that determine how the state should be updated in response to optimistic mutations.

    Parameters

    • state

      T

      The current state, equal to the passthrough value when no reducers have been applied.

    • action

      OptimisticReducerAction<U>

      The action received by the reducer, used to optimistically update the state.

Reducer actions

Here is the signature for the action parameter for reducers:

  • The document that was updated

  • id

    string

    The published document ID (the same as the _id)

  • The original document ID

  • type

    'appear' | 'mutate' | 'disappear

    The type of action occurring (only mutate is currently supported)

Was this page helpful?