Skip to content

Commit

Permalink
Cleanup enableUseRefAccessWarning flag (#28699)
Browse files Browse the repository at this point in the history
Cleanup enableUseRefAccessWarning flag

I don't think this flag has a path forward in the current
implementation. The detection by stack trace is too brittle to detect
the lazy initialization pattern reliably (see e.g. some internal tests
that expect the warning because they use lazy intialization, but a
slightly different pattern then the expected pattern.

I think a new version of this could be to fully ban ref access during
render with an alternative API for the exceptional cases that today
require ref access during render.

DiffTrain build for commit 20e710a.
  • Loading branch information
kassens committed Apr 3, 2024
1 parent 0092358 commit 6935054
Show file tree
Hide file tree
Showing 8 changed files with 334 additions and 527 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @noflow
* @nolint
* @preventMunge
* @generated SignedSource<<9049da9d0398addbe9ad47567cb0a0f2>>
* @generated SignedSource<<dff5882f8219f97fa6b685d700750e06>>
*/

"use strict";
Expand Down Expand Up @@ -9818,14 +9818,11 @@ if (__DEV__) {

function mountRef(initialValue) {
var hook = mountWorkInProgressHook();

{
var _ref2 = {
current: initialValue
};
hook.memoizedState = _ref2;
return _ref2;
}
var ref = {
current: initialValue
};
hook.memoizedState = ref;
return ref;
}

function updateRef(initialValue) {
Expand Down Expand Up @@ -26801,7 +26798,7 @@ if (__DEV__) {
return root;
}

var ReactVersion = "19.0.0-canary-23a1aa6f";
var ReactVersion = "19.0.0-canary-9dd5e9f4";

// Might add PROFILE later.

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7a2609eedc571049a3272e60d5f7d84601ffca3f
20e710aeab3e03809c82d134171986ea270026a0
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @noflow
* @nolint
* @preventMunge
* @generated SignedSource<<83ce9566209d54efec3736b91598c233>>
* @generated SignedSource<<cb885e8934a011f045924ca7dbd54aba>>
*/

"use strict";
Expand Down Expand Up @@ -2984,7 +2984,6 @@ to return true:wantsResponderID| |
dynamicFlags.enableInfiniteRenderLoopDetection,
enableRenderableContext = dynamicFlags.enableRenderableContext,
enableUnifiedSyncLane = dynamicFlags.enableUnifiedSyncLane,
enableUseRefAccessWarning = dynamicFlags.enableUseRefAccessWarning,
passChildrenWhenCloningPersistedNodes =
dynamicFlags.passChildrenWhenCloningPersistedNodes,
useModernStrictMode = dynamicFlags.useModernStrictMode; // The rest of the flags are static for better dead code elimination.
Expand Down Expand Up @@ -13309,95 +13308,13 @@ to return true:wantsResponderID| |
};
}

var stackContainsErrorMessage = null;

function getCallerStackFrame() {
// eslint-disable-next-line react-internal/prod-error-codes
var stackFrames = new Error("Error message").stack.split("\n"); // Some browsers (e.g. Chrome) include the error message in the stack
// but others (e.g. Firefox) do not.

if (stackContainsErrorMessage === null) {
stackContainsErrorMessage = stackFrames[0].includes("Error message");
}

return stackContainsErrorMessage
? stackFrames.slice(3, 4).join("\n")
: stackFrames.slice(2, 3).join("\n");
}

function mountRef(initialValue) {
var hook = mountWorkInProgressHook();

if (enableUseRefAccessWarning) {
{
// Support lazy initialization pattern shown in docs.
// We need to store the caller stack frame so that we don't warn on subsequent renders.
var hasBeenInitialized = initialValue != null;
var lazyInitGetterStack = null;
var didCheckForLazyInit = false; // Only warn once per component+hook.

var didWarnAboutRead = false;
var didWarnAboutWrite = false;
var current = initialValue;
var ref = {
get current() {
if (!hasBeenInitialized) {
didCheckForLazyInit = true;
lazyInitGetterStack = getCallerStackFrame();
} else if (
currentlyRenderingFiber$1 !== null &&
!didWarnAboutRead
) {
if (
lazyInitGetterStack === null ||
lazyInitGetterStack !== getCallerStackFrame()
) {
didWarnAboutRead = true;

warn(
"%s: Unsafe read of a mutable value during render.\n\n" +
"Reading from a ref during render is only safe if:\n" +
"1. The ref value has not been updated, or\n" +
"2. The ref holds a lazily-initialized value that is only set once.\n",
getComponentNameFromFiber(currentlyRenderingFiber$1) ||
"Unknown"
);
}
}

return current;
},

set current(value) {
if (currentlyRenderingFiber$1 !== null && !didWarnAboutWrite) {
if (hasBeenInitialized || !didCheckForLazyInit) {
didWarnAboutWrite = true;

warn(
"%s: Unsafe write of a mutable value during render.\n\n" +
"Writing to a ref during render is only safe if the ref holds " +
"a lazily-initialized value that is only set once.\n",
getComponentNameFromFiber(currentlyRenderingFiber$1) ||
"Unknown"
);
}
}

hasBeenInitialized = true;
current = value;
}
};
Object.seal(ref);
hook.memoizedState = ref;
return ref;
}
} else {
var _ref2 = {
current: initialValue
};
hook.memoizedState = _ref2;
return _ref2;
}
var ref = {
current: initialValue
};
hook.memoizedState = ref;
return ref;
}

function updateRef(initialValue) {
Expand Down Expand Up @@ -30572,7 +30489,7 @@ to return true:wantsResponderID| |
return root;
}

var ReactVersion = "19.0.0-canary-d5e5d620";
var ReactVersion = "19.0.0-canary-dbb66dd5";

function createPortal$1(
children,
Expand Down
Loading

0 comments on commit 6935054

Please sign in to comment.