Fix SSR useCallback in render phase (#14279)

This commit is contained in:
Dan Abramov 2018-11-19 20:47:38 +00:00 committed by GitHub
parent 0e9cb3f5d0
commit ccb14e270c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 2 deletions

View File

@ -516,6 +516,19 @@ describe('ReactDOMServerHooks', () => {
expect(domNode.tagName).toEqual('SPAN');
expect(domNode.textContent).toEqual('Count: 0');
});
itRenders('should support render time callbacks', async render => {
function Counter(props) {
const renderCount = useCallback(increment => {
return 'Count: ' + (props.count + increment);
});
return <Text text={renderCount(3)} />;
}
const domNode = await render(<Counter count={2} />);
expect(clearYields()).toEqual(['Count: 5']);
expect(domNode.tagName).toEqual('SPAN');
expect(domNode.textContent).toEqual('Count: 5');
});
});
describe('useImperativeMethods', () => {

View File

@ -341,6 +341,9 @@ function dispatchAction<A>(
}
function noop(): void {}
function identity(fn: Function): Function {
return fn;
}
export let currentThreadID: ThreadID = 0;
@ -357,10 +360,10 @@ export const Dispatcher = {
useState,
useMutationEffect,
useLayoutEffect,
// Callbacks are passed as they are in the server environment.
useCallback: identity,
// useImperativeMethods is not run in the server environment
useImperativeMethods: noop,
// Callbacks are not run in the server environment.
useCallback: noop,
// Effects are not run in the server environment.
useEffect: noop,
};