Skip to content

Commit efad8ac

Browse files
committed
Remove unstable_changedBits
We added this unstable feature a few years ago, as a way to opt out of context updates, but it didn't prove useful in practice. We have other proposals for how to address the same problem, like context selectors. Since it was prefixed with `unstable_`, we should be able to remove it without consequence. The hook API already warned if you used it. Even if someone is using it somewhere, it's meant to be an optimization only, so if they are using the API properly, it should not have any semantic impact.
1 parent 5fe091c commit efad8ac

19 files changed

+123
-720
lines changed

packages/react-cache/src/ReactCacheOld.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const ReactCurrentDispatcher =
4646
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
4747
.ReactCurrentDispatcher;
4848

49-
function readContext(Context, observedBits) {
49+
function readContext(Context) {
5050
const dispatcher = ReactCurrentDispatcher.current;
5151
if (dispatcher === null) {
5252
throw new Error(
@@ -55,7 +55,7 @@ function readContext(Context, observedBits) {
5555
'lifecycle methods.',
5656
);
5757
}
58-
return dispatcher.readContext(Context, observedBits);
58+
return dispatcher.readContext(Context);
5959
}
6060

6161
function identityHashFn(input) {

packages/react-debug-tools/src/ReactDebugHooks.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,12 @@ function getCacheForType<T>(resourceType: () => T): T {
109109
invariant(false, 'Not implemented.');
110110
}
111111

112-
function readContext<T>(
113-
context: ReactContext<T>,
114-
observedBits: void | number | boolean,
115-
): T {
112+
function readContext<T>(context: ReactContext<T>): T {
116113
// For now we don't expose readContext usage in the hooks debugging info.
117114
return context._currentValue;
118115
}
119116

120-
function useContext<T>(
121-
context: ReactContext<T>,
122-
observedBits: void | number | boolean,
123-
): T {
117+
function useContext<T>(context: ReactContext<T>): T {
124118
hookLog.push({
125119
primitive: 'Context',
126120
stackError: new Error(),

packages/react-devtools-shared/src/devtools/cache.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const Rejected = 2;
6161
const ReactCurrentDispatcher = (React: any)
6262
.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentDispatcher;
6363

64-
function readContext(Context, observedBits) {
64+
function readContext(Context) {
6565
const dispatcher = ReactCurrentDispatcher.current;
6666
if (dispatcher === null) {
6767
throw new Error(
@@ -70,7 +70,7 @@ function readContext(Context, observedBits) {
7070
'lifecycle methods.',
7171
);
7272
}
73-
return dispatcher.readContext(Context, observedBits);
73+
return dispatcher.readContext(Context);
7474
}
7575

7676
const CacheContext = createContext(null);

packages/react-dom/src/__tests__/ReactDOMServerIntegrationHooks-test.js

+2-13
Original file line numberDiff line numberDiff line change
@@ -736,17 +736,6 @@ describe('ReactDOMServerHooks', () => {
736736
},
737737
);
738738

739-
itRenders('warns when bitmask is passed to useContext', async render => {
740-
const Context = React.createContext('Hi');
741-
742-
function Foo() {
743-
return <span>{useContext(Context, 1)}</span>;
744-
}
745-
746-
const domNode = await render(<Foo />, 1);
747-
expect(domNode.textContent).toBe('Hi');
748-
});
749-
750739
describe('useDebugValue', () => {
751740
itRenders('is a noop', async render => {
752741
function Counter(props) {
@@ -760,11 +749,11 @@ describe('ReactDOMServerHooks', () => {
760749
});
761750

762751
describe('readContext', () => {
763-
function readContext(Context, observedBits) {
752+
function readContext(Context) {
764753
const dispatcher =
765754
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
766755
.ReactCurrentDispatcher.current;
767-
return dispatcher.readContext(Context, observedBits);
756+
return dispatcher.readContext(Context);
768757
}
769758

770759
itRenders(

packages/react-dom/src/__tests__/ReactDOMServerIntegrationNewContext-test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,11 @@ describe('ReactDOMServerIntegration', () => {
161161
});
162162

163163
itRenders('readContext() in different components', async render => {
164-
function readContext(Ctx, observedBits) {
164+
function readContext(Ctx) {
165165
const dispatcher =
166166
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
167167
.ReactCurrentDispatcher.current;
168-
return dispatcher.readContext(Ctx, observedBits);
168+
return dispatcher.readContext(Ctx);
169169
}
170170

171171
class Cls extends React.Component {

packages/react-dom/src/server/ReactPartialRendererHooks.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,7 @@ function getCacheForType<T>(resourceType: () => T): T {
219219
invariant(false, 'Not implemented.');
220220
}
221221

222-
function readContext<T>(
223-
context: ReactContext<T>,
224-
observedBits: void | number | boolean,
225-
): T {
222+
function readContext<T>(context: ReactContext<T>): T {
226223
const threadID = currentPartialRenderer.threadID;
227224
validateContextBounds(context, threadID);
228225
if (__DEV__) {
@@ -238,10 +235,7 @@ function readContext<T>(
238235
return context[threadID];
239236
}
240237

241-
function useContext<T>(
242-
context: ReactContext<T>,
243-
observedBits: void | number | boolean,
244-
): T {
238+
function useContext<T>(context: ReactContext<T>): T {
245239
if (__DEV__) {
246240
currentHookNameInDev = 'useContext';
247241
}

packages/react-reconciler/src/ReactFiberBeginWork.new.js

+6-23
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ import {
160160
checkIfContextChanged,
161161
readContext,
162162
prepareToReadContext,
163-
calculateChangedBits,
164163
scheduleWorkOnParentPath,
165164
} from './ReactFiberNewContext.new';
166165
import {renderWithHooks, bailoutHooks} from './ReactFiberHooks.new';
@@ -221,7 +220,7 @@ import {
221220
restoreSpawnedCachePool,
222221
getOffscreenDeferredCachePool,
223222
} from './ReactFiberCacheComponent.new';
224-
import {MAX_SIGNED_31_BIT_INT} from './MaxInts';
223+
import is from 'shared/objectIs';
225224

226225
import {disableLogs, reenableLogs} from 'shared/ConsolePatchingDev';
227226

@@ -796,12 +795,7 @@ function updateCacheComponent(
796795
pushCacheProvider(workInProgress, nextCache);
797796
if (nextCache !== prevState.cache) {
798797
// This cache refreshed. Propagate a context change.
799-
propagateContextChange(
800-
workInProgress,
801-
CacheContext,
802-
MAX_SIGNED_31_BIT_INT,
803-
renderLanes,
804-
);
798+
propagateContextChange(workInProgress, CacheContext, renderLanes);
805799
}
806800
}
807801
}
@@ -1168,12 +1162,7 @@ function updateHostRoot(current, workInProgress, renderLanes) {
11681162
pushCacheProvider(workInProgress, nextCache);
11691163
if (nextCache !== prevState.cache) {
11701164
// The root cache refreshed.
1171-
propagateContextChange(
1172-
workInProgress,
1173-
CacheContext,
1174-
MAX_SIGNED_31_BIT_INT,
1175-
renderLanes,
1176-
);
1165+
propagateContextChange(workInProgress, CacheContext, renderLanes);
11771166
}
11781167
}
11791168

@@ -3007,8 +2996,7 @@ function updateContextProvider(
30072996
} else {
30082997
if (oldProps !== null) {
30092998
const oldValue = oldProps.value;
3010-
const changedBits = calculateChangedBits(context, newValue, oldValue);
3011-
if (changedBits === 0) {
2999+
if (is(oldValue, newValue)) {
30123000
// No change. Bailout early if children are the same.
30133001
if (
30143002
oldProps.children === newProps.children &&
@@ -3023,12 +3011,7 @@ function updateContextProvider(
30233011
} else {
30243012
// The context value changed. Search for matching consumers and schedule
30253013
// them to update.
3026-
propagateContextChange(
3027-
workInProgress,
3028-
context,
3029-
changedBits,
3030-
renderLanes,
3031-
);
3014+
propagateContextChange(workInProgress, context, renderLanes);
30323015
}
30333016
}
30343017
}
@@ -3086,7 +3069,7 @@ function updateContextConsumer(
30863069
}
30873070

30883071
prepareToReadContext(workInProgress, renderLanes);
3089-
const newValue = readContext(context, newProps.unstable_observedBits);
3072+
const newValue = readContext(context);
30903073
let newChildren;
30913074
if (__DEV__) {
30923075
ReactCurrentOwner.current = workInProgress;

packages/react-reconciler/src/ReactFiberBeginWork.old.js

+6-23
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ import {
160160
checkIfContextChanged,
161161
readContext,
162162
prepareToReadContext,
163-
calculateChangedBits,
164163
scheduleWorkOnParentPath,
165164
} from './ReactFiberNewContext.old';
166165
import {renderWithHooks, bailoutHooks} from './ReactFiberHooks.old';
@@ -221,7 +220,7 @@ import {
221220
restoreSpawnedCachePool,
222221
getOffscreenDeferredCachePool,
223222
} from './ReactFiberCacheComponent.old';
224-
import {MAX_SIGNED_31_BIT_INT} from './MaxInts';
223+
import is from 'shared/objectIs';
225224

226225
import {disableLogs, reenableLogs} from 'shared/ConsolePatchingDev';
227226

@@ -796,12 +795,7 @@ function updateCacheComponent(
796795
pushCacheProvider(workInProgress, nextCache);
797796
if (nextCache !== prevState.cache) {
798797
// This cache refreshed. Propagate a context change.
799-
propagateContextChange(
800-
workInProgress,
801-
CacheContext,
802-
MAX_SIGNED_31_BIT_INT,
803-
renderLanes,
804-
);
798+
propagateContextChange(workInProgress, CacheContext, renderLanes);
805799
}
806800
}
807801
}
@@ -1168,12 +1162,7 @@ function updateHostRoot(current, workInProgress, renderLanes) {
11681162
pushCacheProvider(workInProgress, nextCache);
11691163
if (nextCache !== prevState.cache) {
11701164
// The root cache refreshed.
1171-
propagateContextChange(
1172-
workInProgress,
1173-
CacheContext,
1174-
MAX_SIGNED_31_BIT_INT,
1175-
renderLanes,
1176-
);
1165+
propagateContextChange(workInProgress, CacheContext, renderLanes);
11771166
}
11781167
}
11791168

@@ -3007,8 +2996,7 @@ function updateContextProvider(
30072996
} else {
30082997
if (oldProps !== null) {
30092998
const oldValue = oldProps.value;
3010-
const changedBits = calculateChangedBits(context, newValue, oldValue);
3011-
if (changedBits === 0) {
2999+
if (is(oldValue, newValue)) {
30123000
// No change. Bailout early if children are the same.
30133001
if (
30143002
oldProps.children === newProps.children &&
@@ -3023,12 +3011,7 @@ function updateContextProvider(
30233011
} else {
30243012
// The context value changed. Search for matching consumers and schedule
30253013
// them to update.
3026-
propagateContextChange(
3027-
workInProgress,
3028-
context,
3029-
changedBits,
3030-
renderLanes,
3031-
);
3014+
propagateContextChange(workInProgress, context, renderLanes);
30323015
}
30333016
}
30343017
}
@@ -3086,7 +3069,7 @@ function updateContextConsumer(
30863069
}
30873070

30883071
prepareToReadContext(workInProgress, renderLanes);
3089-
const newValue = readContext(context, newProps.unstable_observedBits);
3072+
const newValue = readContext(context);
30903073
let newChildren;
30913074
if (__DEV__) {
30923075
ReactCurrentOwner.current = workInProgress;

packages/react-reconciler/src/ReactFiberCacheComponent.new.js

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export const CacheContext: ReactContext<Cache> = enableCache
3737
// We don't use Consumer/Provider for Cache components. So we'll cheat.
3838
Consumer: (null: any),
3939
Provider: (null: any),
40-
_calculateChangedBits: null,
4140
// We'll initialize these at the root.
4241
_currentValue: (null: any),
4342
_currentValue2: (null: any),

packages/react-reconciler/src/ReactFiberCacheComponent.old.js

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export const CacheContext: ReactContext<Cache> = enableCache
3737
// We don't use Consumer/Provider for Cache components. So we'll cheat.
3838
Consumer: (null: any),
3939
Provider: (null: any),
40-
_calculateChangedBits: null,
4140
// We'll initialize these at the root.
4241
_currentValue: (null: any),
4342
_currentValue2: (null: any),

0 commit comments

Comments
 (0)