Restore the Hooks dispatcher after using SSR (#14105)
Dan Abramov committed
Nov 6, 2018 at 01:15 UTC
600651e68e0686f606aca9fc559f8cdc5f7ed567
3 files changed
+20
-1
packages/react-dom/src/server/ReactPartialRenderer.js
+2
-1
@@ -821,6 +821,7 @@ class ReactDOMServerRenderer {
821
return null;
822
}
823
824
+ const prevDispatcher = ReactCurrentOwner.currentDispatcher;
825
if (enableHooks) {
826
ReactCurrentOwner.currentDispatcher = Dispatcher;
827
} else {
@@ -871,7 +872,7 @@ class ReactDOMServerRenderer {
872
}
873
return out;
874
} finally {
874
- ReactCurrentOwner.currentDispatcher = null;
875
+ ReactCurrentOwner.currentDispatcher = prevDispatcher;
876
}
877
}
878
packages/react-reconciler/src/__tests__/ReactHooks-test.internal.js
+15
@@ -15,6 +15,7 @@
15
let React;
16
let ReactFeatureFlags;
17
let ReactTestRenderer;
18
+let ReactDOMServer;
19
20
// Additional tests can be found in ReactHooksWithNoopRenderer. Plan is to
21
// gradually migrate those to this file.
@@ -27,6 +28,7 @@ describe('ReactHooks', () => {
28
ReactFeatureFlags.enableHooks = true;
29
React = require('react');
30
ReactTestRenderer = require('react-test-renderer');
31
+ ReactDOMServer = require('react-dom/server');
32
});
33
34
it('warns about variable number of dependencies', () => {
@@ -79,4 +81,17 @@ describe('ReactHooks', () => {
81
' in App (at **)',
82
]);
83
});
84
+
85
+ // https://github.com/facebook/react/issues/14022
86
+ it('works with ReactDOMServer calls inside a component', () => {
87
+ const {useState} = React;
88
+ function App(props) {
89
+ const markup1 = ReactDOMServer.renderToString(<p>hello</p>);
90
+ const markup2 = ReactDOMServer.renderToStaticMarkup(<p>bye</p>);
91
+ const [counter] = useState(0);
92
+ return markup1 + counter + markup2;
93
+ }
94
+ const root = ReactTestRenderer.create(<App />);
95
+ expect(root.toJSON()).toMatchSnapshot();
96
+ });
97
});
packages/react-reconciler/src/__tests__/__snapshots__/ReactHooks-test.internal.js.snap
new
+3
@@ -0,0 +1,3 @@
1
+// Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+exports[`ReactHooks works with ReactDOMServer calls inside a component 1`] = `"<p data-reactroot=\\"\\">hello</p>0<p>bye</p>"`;