MCPcopy Create free account
hub / github.com/RobPruzan/react-scratch / useRef

Function useRef

src/react/core.ts:1379–1417  ·  view source on GitHub ↗
(initialValue: T)

Source from the content-addressed store, hash-verified

1377};
1378
1379export const useRef = <T>(initialValue: T) => {
1380 if (!currentTreeRef.renderTree) {
1381 throw new Error("Cannot call use state outside of a react component");
1382 }
1383 if (!currentTreeRef.renderTree.currentlyRendering) {
1384 throw new Error("Component being called outside of react internals");
1385 }
1386
1387 const currentStateOrder =
1388 currentTreeRef.renderTree.currentLocalCurrentHookOrder;
1389 currentTreeRef.renderTree.currentLocalCurrentHookOrder += 1;
1390 const currentlyRendering = currentTreeRef.renderTree?.currentlyRendering;
1391
1392 if (!currentlyRendering) {
1393 throw new Error("Cannot call use state outside of a react component");
1394 }
1395
1396 if (currentlyRendering.kind === "empty-slot") {
1397 throw new Error("A slot will never call a hook");
1398 }
1399
1400 if (!currentlyRendering.hasRendered) {
1401 const refTo = {
1402 current: initialValue,
1403 };
1404 currentlyRendering.hooks.push({
1405 kind: "ref",
1406 refTo,
1407 });
1408 return refTo;
1409 }
1410
1411 const hookValue = currentlyRendering.hooks[currentStateOrder];
1412 if (hookValue.kind !== "ref") {
1413 throw new Error("Different hooks called compared previous render");
1414 }
1415
1416 return hookValue.refTo as { current: T };
1417};
1418
1419// this is wrong, need to potentially update deps and cb
1420export const useEffect = (cb: () => void, deps: Array<unknown>) => {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected