Skip to content

Commit

Permalink
Remove unstable_changedBits
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
acdlite committed Mar 8, 2021
1 parent 5fe091c commit 6c4d404
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 697 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -736,17 +736,6 @@ describe('ReactDOMServerHooks', () => {
},
);

itRenders('warns when bitmask is passed to useContext', async render => {
const Context = React.createContext('Hi');

function Foo() {
return <span>{useContext(Context, 1)}</span>;
}

const domNode = await render(<Foo />, 1);
expect(domNode.textContent).toBe('Hi');
});

describe('useDebugValue', () => {
itRenders('is a noop', async render => {
function Counter(props) {
Expand All @@ -760,11 +749,11 @@ describe('ReactDOMServerHooks', () => {
});

describe('readContext', () => {
function readContext(Context, observedBits) {
function readContext(Context) {
const dispatcher =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.ReactCurrentDispatcher.current;
return dispatcher.readContext(Context, observedBits);
return dispatcher.readContext(Context);
}

itRenders(
Expand Down
29 changes: 6 additions & 23 deletions packages/react-reconciler/src/ReactFiberBeginWork.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ import {
checkIfContextChanged,
readContext,
prepareToReadContext,
calculateChangedBits,
scheduleWorkOnParentPath,
} from './ReactFiberNewContext.new';
import {renderWithHooks, bailoutHooks} from './ReactFiberHooks.new';
Expand Down Expand Up @@ -221,7 +220,7 @@ import {
restoreSpawnedCachePool,
getOffscreenDeferredCachePool,
} from './ReactFiberCacheComponent.new';
import {MAX_SIGNED_31_BIT_INT} from './MaxInts';
import is from 'shared/objectIs';

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

Expand Down Expand Up @@ -796,12 +795,7 @@ function updateCacheComponent(
pushCacheProvider(workInProgress, nextCache);
if (nextCache !== prevState.cache) {
// This cache refreshed. Propagate a context change.
propagateContextChange(
workInProgress,
CacheContext,
MAX_SIGNED_31_BIT_INT,
renderLanes,
);
propagateContextChange(workInProgress, CacheContext, renderLanes);
}
}
}
Expand Down Expand Up @@ -1168,12 +1162,7 @@ function updateHostRoot(current, workInProgress, renderLanes) {
pushCacheProvider(workInProgress, nextCache);
if (nextCache !== prevState.cache) {
// The root cache refreshed.
propagateContextChange(
workInProgress,
CacheContext,
MAX_SIGNED_31_BIT_INT,
renderLanes,
);
propagateContextChange(workInProgress, CacheContext, renderLanes);
}
}

Expand Down Expand Up @@ -3007,8 +2996,7 @@ function updateContextProvider(
} else {
if (oldProps !== null) {
const oldValue = oldProps.value;
const changedBits = calculateChangedBits(context, newValue, oldValue);
if (changedBits === 0) {
if (is(oldValue, newValue)) {
// No change. Bailout early if children are the same.
if (
oldProps.children === newProps.children &&
Expand All @@ -3023,12 +3011,7 @@ function updateContextProvider(
} else {
// The context value changed. Search for matching consumers and schedule
// them to update.
propagateContextChange(
workInProgress,
context,
changedBits,
renderLanes,
);
propagateContextChange(workInProgress, context, renderLanes);
}
}
}
Expand Down Expand Up @@ -3086,7 +3069,7 @@ function updateContextConsumer(
}

prepareToReadContext(workInProgress, renderLanes);
const newValue = readContext(context, newProps.unstable_observedBits);
const newValue = readContext(context);
let newChildren;
if (__DEV__) {
ReactCurrentOwner.current = workInProgress;
Expand Down
29 changes: 6 additions & 23 deletions packages/react-reconciler/src/ReactFiberBeginWork.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ import {
checkIfContextChanged,
readContext,
prepareToReadContext,
calculateChangedBits,
scheduleWorkOnParentPath,
} from './ReactFiberNewContext.old';
import {renderWithHooks, bailoutHooks} from './ReactFiberHooks.old';
Expand Down Expand Up @@ -221,7 +220,7 @@ import {
restoreSpawnedCachePool,
getOffscreenDeferredCachePool,
} from './ReactFiberCacheComponent.old';
import {MAX_SIGNED_31_BIT_INT} from './MaxInts';
import is from 'shared/objectIs';

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

Expand Down Expand Up @@ -796,12 +795,7 @@ function updateCacheComponent(
pushCacheProvider(workInProgress, nextCache);
if (nextCache !== prevState.cache) {
// This cache refreshed. Propagate a context change.
propagateContextChange(
workInProgress,
CacheContext,
MAX_SIGNED_31_BIT_INT,
renderLanes,
);
propagateContextChange(workInProgress, CacheContext, renderLanes);
}
}
}
Expand Down Expand Up @@ -1168,12 +1162,7 @@ function updateHostRoot(current, workInProgress, renderLanes) {
pushCacheProvider(workInProgress, nextCache);
if (nextCache !== prevState.cache) {
// The root cache refreshed.
propagateContextChange(
workInProgress,
CacheContext,
MAX_SIGNED_31_BIT_INT,
renderLanes,
);
propagateContextChange(workInProgress, CacheContext, renderLanes);
}
}

Expand Down Expand Up @@ -3007,8 +2996,7 @@ function updateContextProvider(
} else {
if (oldProps !== null) {
const oldValue = oldProps.value;
const changedBits = calculateChangedBits(context, newValue, oldValue);
if (changedBits === 0) {
if (is(oldValue, newValue)) {
// No change. Bailout early if children are the same.
if (
oldProps.children === newProps.children &&
Expand All @@ -3023,12 +3011,7 @@ function updateContextProvider(
} else {
// The context value changed. Search for matching consumers and schedule
// them to update.
propagateContextChange(
workInProgress,
context,
changedBits,
renderLanes,
);
propagateContextChange(workInProgress, context, renderLanes);
}
}
}
Expand Down Expand Up @@ -3086,7 +3069,7 @@ function updateContextConsumer(
}

prepareToReadContext(workInProgress, renderLanes);
const newValue = readContext(context, newProps.unstable_observedBits);
const newValue = readContext(context);
let newChildren;
if (__DEV__) {
ReactCurrentOwner.current = workInProgress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const CacheContext: ReactContext<Cache> = enableCache
// We don't use Consumer/Provider for Cache components. So we'll cheat.
Consumer: (null: any),
Provider: (null: any),
_calculateChangedBits: null,
// We'll initialize these at the root.
_currentValue: (null: any),
_currentValue2: (null: any),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const CacheContext: ReactContext<Cache> = enableCache
// We don't use Consumer/Provider for Cache components. So we'll cheat.
Consumer: (null: any),
Provider: (null: any),
_calculateChangedBits: null,
// We'll initialize these at the root.
_currentValue: (null: any),
_currentValue2: (null: any),
Expand Down
Loading

0 comments on commit 6c4d404

Please sign in to comment.