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

Function use

packages/react-debug-tools/src/ReactDebugHooks.js:202–265  ·  packages/react-debug-tools/src/ReactDebugHooks.js::use
(usable: Usable<T>)

Source from the content-addressed store, hash-verified

200);
201
202function use<T>(usable: Usable<T>): T {
203 if (usable !== null && typeof usable === class="st">'object') {
204 class="cm">// $FlowFixMe[method-unbinding]
205 if (typeof usable.then === class="st">'function') {
206 const thenable: Thenable<any> =
207 class="cm">// If we have thenable state, then the actually used thenable will be the one
208 class="cm">// stashed in it. It's possible for uncached Promises to be new each render
209 class="cm">// and in that case the one we're inspecting is the in the thenable state.
210 currentThenableState !== null &&
211 currentThenableIndex < currentThenableState.length
212 ? currentThenableState[currentThenableIndex++]
213 : (usable: any);
214
215 switch (thenable.status) {
216 case class="st">'fulfilled': {
217 const fulfilledValue: T = thenable.value;
218 hookLog.push({
219 displayName: null,
220 primitive: class="st">'Promise',
221 stackError: new Error(),
222 value: fulfilledValue,
223 debugInfo:
224 thenable._debugInfo === undefined ? null : thenable._debugInfo,
225 dispatcherHookName: class="st">'Use',
226 });
227 return fulfilledValue;
228 }
229 case class="st">'rejected': {
230 const rejectedError = thenable.reason;
231 throw rejectedError;
232 }
233 }
234 class="cm">// If this was an uncached Promise we have to abandon this attempt
235 class="cm">// but we can still emit anything up until this point.
236 hookLog.push({
237 displayName: null,
238 primitive: class="st">'Unresolved',
239 stackError: new Error(),
240 value: thenable,
241 debugInfo:
242 thenable._debugInfo === undefined ? null : thenable._debugInfo,
243 dispatcherHookName: class="st">'Use',
244 });
245 throw SuspenseException;
246 } else if (usable.$$typeof === REACT_CONTEXT_TYPE) {
247 const context: ReactContext<T> = (usable: any);
248 const value = readContext(context);
249
250 hookLog.push({
251 displayName: context.displayName || class="st">'Context',
252 primitive: class="st">'Context (use)',
253 stackError: new Error(),
254 value,
255 debugInfo: null,
256 dispatcherHookName: class="st">'Use',
257 });
258
259 return value;

Callers 15

FunctionWithHooksFunction · 0.90
useInferredNameFunction · 0.90
StackTraceView.jsFile · 0.90
DebuggingSettingsFunction · 0.90
AsyncFunction · 0.50
ClientFunction · 0.50
ChildFunction · 0.50
AppFunction · 0.50
FirstFunction · 0.50
SecondFunction · 0.50

Calls 2

readContextFunction · 0.70
pushMethod · 0.65

Tested by 8

AsyncFunction · 0.40
ClientFunction · 0.40
ChildFunction · 0.40
AppFunction · 0.40
FirstFunction · 0.40
SecondFunction · 0.40
HangingFunction · 0.40
AppFunction · 0.40