dillon1000/react
Commit
Browse files Manually revert #2127 (allow mutating context in callbacks)
#2127 introduced a special type for the result of `useContext()` that was sort
of ref-like. The intent was to allow code like this:
```
function Foo() {
const cx = useContext(...);
function onEvent() {
cx.foo = true;
};
return <Bar onEvent={onEvent} />;
}
```
However, that code actually is allowed by the compiler by default. It's only a
bailout when `@validateFrozenLambdas` is enabled. The "fix" in #2127 therefore
wasn't strictly necessary to unblock rollout, and it's also flawed in a few
ways:
* First, `useContext(FooContext)` should have equivalent behavior to a custom
hooks which does the same thing, ie `function useFooContext() { return
useContext(FooContext) }`. Specializing the type of useContext makes the
behavior different.
* Second, it meant that even readonly accesses of the context inside a callback
marked the function as capturing, which in turn prevented those callbacks from
being memoized.
So i'm reverting this and we'll have to think a bit more about this case.Changed paths17 files
First-parent comparisoncompiler/packages/babel-plugin-react-forget/src/HIR/Globals.ts ModifiedM compiler/packages/babel-plugin-react-forget/src/HIR/HIR.ts ModifiedM compiler/packages/babel-plugin-react-forget/src/HIR/ObjectShape.ts ModifiedM compiler/packages/babel-plugin-react-forget/src/Inference/AnalyseFunctions.ts ModifiedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/maybe-mutate-object-in-callback.expect.md AddedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/maybe-mutate-object-in-callback.js AddedM compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/react-namespace.expect.md ModifiedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/todo.useContext-mutate-context-in-callback.js AddedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useContext-maybe-mutate-context-in-callback.expect.md AddedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useContext-maybe-mutate-context-in-callback.js AddedD compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useContext-mutable-value.expect.md DeletedD compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useContext-mutable-value.js DeletedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useContext-read-context-in-callback-if-condition.expect.md AddedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useContext-read-context-in-callback-if-condition.js AddedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useContext-read-context-in-callback.expect.md AddedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useContext-read-context-in-callback.js AddedM compiler/packages/sprout/src/SproutTodoFilter.ts ModifiedPatch
Files changed
Rendering syntax-highlighted changes…