Add readContext to ReactPartialRendererHooks

This commit is contained in:
Alex Taylor
2018-10-29 11:26:54 -07:00
committed by Andrew Clark
parent f7cb9d2b22
commit 9f34eb79a3
2 changed files with 25 additions and 1 deletions
@@ -646,5 +646,21 @@ describe('ReactDOMServerHooks', () => {
expect(domNode.lastChild.textContent).toEqual('Baz: 5');
},
);
itThrowsWhenRendering(
'if used inside a class component',
async render => {
const Context = React.createContext({}, () => {});
class Counter extends React.Component {
render() {
let [count] = useContext(Context);
return <Text text={count} />;
}
}
return render(<Counter />);
},
'Hooks can only be called inside the body of a function component.',
);
});
});
+9 -1
View File
@@ -138,10 +138,18 @@ export function finishHooks(
return children;
}
function readContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
): T {
return context._currentValue;
}
function useContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
): T {
resolveCurrentlyRenderingComponent();
return context._currentValue;
}
@@ -349,7 +357,7 @@ function inputsAreEqual(arr1, arr2) {
function noop(): void {}
export const Dispatcher = {
readContext: useContext,
readContext,
useContext,
useMemo,
useReducer,