Skip to content

Commit afb8d3b

Browse files
committed
[change] Configure hydration using AppRegistry.runApplication()
Client-side hydration of server-rendered HTML now requires that `hydrate` is explicitly set in the `appParams` passed to `AppRegistry.runApplication()`. Fix #1374
1 parent e4ed0fd commit afb8d3b

File tree

4 files changed

+31
-18
lines changed

4 files changed

+31
-18
lines changed

packages/docs/src/apis/AppRegistry/AppRegistry.stories.mdx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,17 @@ AppRegistry.registerRunnable('MyApp', (appParams) => { ... });
7676
7777
### runApplication(appKey, appParams)
7878
79-
Runs the application that was registered under `appKey`. The `appParameters` must
80-
include the `rootTag` into which the application is rendered, and optionally any
81-
`initialProps` or render callback.
79+
Runs the application that was registered under `appKey`. The `appParameters`
80+
must include the `rootTag` into which the application is rendered, and
81+
optionally any `initialProps` or render callback. If the client should hydrate
82+
server-rendered HTML, set `hydrate` to `true`.
8283
8384
```js
8485
AppRegistry.runApplication('MyApp', {
86+
callback: () => { console.log('React rendering has finished') },
87+
hydrate: true,
8588
initialProps: {},
8689
rootTag: document.getElementById('react-root'),
87-
callback: () => { console.log('React rendering has finished') }
8890
})
8991
```
9092

packages/react-native-web/src/exports/AppRegistry/__tests__/index-test.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,23 @@ describe('AppRegistry', () => {
7373
});
7474

7575
describe('runApplication', () => {
76-
test('callback after render', () => {
77-
// setup
78-
const rootTag = document.createElement('div');
76+
let rootTag;
77+
78+
beforeEach(() => {
79+
rootTag = document.createElement('div');
7980
rootTag.id = 'react-root';
8081
document.body.appendChild(rootTag);
82+
});
8183

84+
afterEach(() => {
85+
document.body.removeChild(rootTag);
86+
});
87+
88+
test('callback after render', () => {
8289
const callback = jest.fn();
8390
AppRegistry.registerComponent('App', () => NoopComponent);
8491
AppRegistry.runApplication('App', { initialProps: {}, rootTag, callback });
8592
expect(callback).toHaveBeenCalledTimes(1);
86-
87-
// cleanup
88-
document.body.removeChild(rootTag);
8993
});
9094
});
9195
});

packages/react-native-web/src/exports/AppRegistry/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,13 @@ export default class AppRegistry {
6363
run: appParameters =>
6464
renderApplication(
6565
componentProviderInstrumentationHook(componentProvider),
66-
appParameters.initialProps || emptyObject,
67-
appParameters.rootTag,
6866
wrapperComponentProvider && wrapperComponentProvider(appParameters),
69-
appParameters.callback
67+
appParameters.callback,
68+
{
69+
hydrate: appParameters.hydrate || false,
70+
initialProps: appParameters.initialProps || emptyObject,
71+
rootTag: appParameters.rootTag
72+
}
7073
)
7174
};
7275
return appKey;

packages/react-native-web/src/exports/AppRegistry/renderApplication.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@ import render from '../render';
1515
import styleResolver from '../StyleSheet/styleResolver';
1616
import React, { type ComponentType } from 'react';
1717

18-
const renderFn = process.env.NODE_ENV !== 'production' ? render : hydrate;
19-
2018
export default function renderApplication<Props: Object>(
2119
RootComponent: ComponentType<Props>,
22-
initialProps: Props,
23-
rootTag: any,
2420
WrapperComponent?: ?ComponentType<*>,
25-
callback?: () => void
21+
callback?: () => void,
22+
options: {
23+
hydrate: boolean,
24+
initialProps: Props,
25+
rootTag: any
26+
}
2627
) {
28+
const { hydrate: shouldHydrate, initialProps, rootTag } = options;
29+
const renderFn = shouldHydrate ? hydrate : render;
30+
2731
invariant(rootTag, 'Expect to have a valid rootTag, instead got ', rootTag);
2832

2933
renderFn(

0 commit comments

Comments
 (0)