Add readContext to ReactPartialRendererHooks
Alex Taylor committed
Oct 11, 2018 at 10:17 UTC
9f34eb79a3f7df71cd89b247de2e12b41b98f55a
2 files changed
+25
-1
packages/react-dom/src/__tests__/ReactDOMServerIntegrationHooks-test.internal.js
+16
@@ -646,5 +646,21 @@ describe('ReactDOMServerHooks', () => {
646
expect(domNode.lastChild.textContent).toEqual('Baz: 5');
647
},
648
);
649
+
650
+ itThrowsWhenRendering(
651
+ 'if used inside a class component',
652
+ async render => {
653
+ const Context = React.createContext({}, () => {});
654
+ class Counter extends React.Component {
655
+ render() {
656
+ let [count] = useContext(Context);
657
+ return <Text text={count} />;
658
+ }
659
+ }
660
+
661
+ return render(<Counter />);
662
+ },
663
+ 'Hooks can only be called inside the body of a function component.',
664
+ );
665
});
666
});
packages/react-dom/src/server/ReactPartialRendererHooks.js
+9
-1
@@ -138,10 +138,18 @@ export function finishHooks(
138
return children;
139
}
140
141
+function readContext<T>(
142
+ context: ReactContext<T>,
143
+ observedBits: void | number | boolean,
144
+): T {
145
+ return context._currentValue;
146
+}
147
+
148
function useContext<T>(
149
context: ReactContext<T>,
150
observedBits: void | number | boolean,
151
): T {
152
+ resolveCurrentlyRenderingComponent();
153
return context._currentValue;
154
}
155
@@ -349,7 +357,7 @@ function inputsAreEqual(arr1, arr2) {
357
function noop(): void {}
358
359
export const Dispatcher = {
352
- readContext: useContext,
360
+ readContext,
361
useContext,
362
useMemo,
363
useReducer,