Fix SSR useCallback in render phase (#14279)
Dan Abramov committed
Nov 19, 2018 at 20:47 UTC
ccb14e270c3376ed47ca43c0c39722ba6bf6eced
2 files changed
+18
-2
packages/react-dom/src/__tests__/ReactDOMServerIntegrationHooks-test.internal.js
+13
@@ -516,6 +516,19 @@ describe('ReactDOMServerHooks', () => {
516
expect(domNode.tagName).toEqual('SPAN');
517
expect(domNode.textContent).toEqual('Count: 0');
518
});
519
+
520
+ itRenders('should support render time callbacks', async render => {
521
+ function Counter(props) {
522
+ const renderCount = useCallback(increment => {
523
+ return 'Count: ' + (props.count + increment);
524
+ });
525
+ return <Text text={renderCount(3)} />;
526
+ }
527
+ const domNode = await render(<Counter count={2} />);
528
+ expect(clearYields()).toEqual(['Count: 5']);
529
+ expect(domNode.tagName).toEqual('SPAN');
530
+ expect(domNode.textContent).toEqual('Count: 5');
531
+ });
532
});
533
534
describe('useImperativeMethods', () => {
packages/react-dom/src/server/ReactPartialRendererHooks.js
+5
-2
@@ -341,6 +341,9 @@ function dispatchAction<A>(
341
}
342
343
function noop(): void {}
344
+function identity(fn: Function): Function {
345
+ return fn;
346
+}
347
348
export let currentThreadID: ThreadID = 0;
349
@@ -357,10 +360,10 @@ export const Dispatcher = {
360
useState,
361
useMutationEffect,
362
useLayoutEffect,
363
+ // Callbacks are passed as they are in the server environment.
364
+ useCallback: identity,
365
// useImperativeMethods is not run in the server environment
366
useImperativeMethods: noop,
362
- // Callbacks are not run in the server environment.
363
- useCallback: noop,
367
// Effects are not run in the server environment.
368
useEffect: noop,
369
};