mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Add readContext to ReactPartialRendererHooks
This commit is contained in:
committed by
Andrew Clark
parent
f7cb9d2b22
commit
9f34eb79a3
@@ -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.',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user