MCPcopy
hub / github.com/facebook/react / useActionState

Function useActionState

packages/react-debug-tools/src/ReactDebugHooks.js:657–725  ·  packages/react-debug-tools/src/ReactDebugHooks.js::useActionState
(
  action: (Awaited<S>, P) => S,
  initialState: Awaited<S>,
  permalink?: string,
)

Source from the content-addressed store, hash-verified

655}
656
657function useActionState<S, P>(
658 action: (Awaited<S>, P) => S,
659 initialState: Awaited<S>,
660 permalink?: string,
661): [Awaited<S>, (P) => void, boolean] {
662 const hook = nextHook(); class="cm">// FormState
663 nextHook(); class="cm">// PendingState
664 nextHook(); class="cm">// ActionQueue
665 const stackError = new Error();
666 let value;
667 let debugInfo = null;
668 let error = null;
669
670 if (hook !== null) {
671 const actionResult = hook.memoizedState;
672 if (
673 typeof actionResult === class="st">'object' &&
674 actionResult !== null &&
675 class="cm">// $FlowFixMe[method-unbinding]
676 typeof actionResult.then === class="st">'function'
677 ) {
678 const thenable: Thenable<Awaited<S>> = (actionResult: any);
679 switch (thenable.status) {
680 case class="st">'fulfilled': {
681 value = thenable.value;
682 debugInfo =
683 thenable._debugInfo === undefined ? null : thenable._debugInfo;
684 break;
685 }
686 case class="st">'rejected': {
687 const rejectedError = thenable.reason;
688 error = rejectedError;
689 break;
690 }
691 default:
692 class="cm">// If this was an uncached Promise we have to abandon this attempt
693 class="cm">// but we can still emit anything up until this point.
694 error = SuspenseException;
695 debugInfo =
696 thenable._debugInfo === undefined ? null : thenable._debugInfo;
697 value = thenable;
698 }
699 } else {
700 value = (actionResult: any);
701 }
702 } else {
703 value = initialState;
704 }
705
706 hookLog.push({
707 displayName: null,
708 primitive: class="st">'ActionState',
709 stackError: stackError,
710 value: value,
711 debugInfo: debugInfo,
712 dispatcherHookName: class="st">'ActionState',
713 });
714

Callers 3

FormFunction · 0.50
AppFunction · 0.50
AppFunction · 0.50

Calls 2

nextHookFunction · 0.85
pushMethod · 0.65

Tested by 3

FormFunction · 0.40
AppFunction · 0.40
AppFunction · 0.40