Skip to content

Commit

Permalink
Use ReactDOM.hydrate() for hydrating a SSR component if available
Browse files Browse the repository at this point in the history
  • Loading branch information
theJoeBiz committed Feb 23, 2018
1 parent eb4438a commit 9e86b75
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Contributors: please follow the recommendations outlined at [keepachangelog.com]
Changes since last non-beta release.

*Please add entries here for your pull requests that are not yet released.*
#### Fixed
- Use ReactDOM.hydrate() for hydrating a SSR component if available. ReactDOM.render() has been deprecated for use on SSR components in React 16 and this addresses the warning. [PR 1028](https://github.com/shakacode/react_on_rails/pull/1028) by [theJoeBiz](https://github.com/theJoeBiz).

### [10.1.1] - 2018-01-26
#### Fixed
Expand Down
10 changes: 8 additions & 2 deletions node_package/src/clientStartup.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ function domNodeIdForEl(el) {
}

/**
* Used for client rendering by ReactOnRails. Either calls ReactDOM.render or delegates
* to a renderer registered by the user.
* Used for client rendering by ReactOnRails. Either calls ReactDOM.hydrate, ReactDOM.render, or
* delegates to a renderer registered by the user.
* @param el
*/
function render(el, railsContext) {
Expand All @@ -115,18 +115,24 @@ function render(el, railsContext) {
return;
}

// Hydrate if available and was server rendered
const shouldHydrate = !!ReactDOM.hydrate && !!domNode.innerHTML;

const reactElementOrRouterResult = createReactElement({
componentObj,
props,
domNodeId,
trace,
railsContext,
shouldHydrate,
});

if (isRouterResult(reactElementOrRouterResult)) {
throw new Error(`\
You returned a server side type of react-router error: ${JSON.stringify(reactElementOrRouterResult)}
You should return a React.Component always for the client side entry point.`);
} else if (shouldHydrate) {
ReactDOM.hydrate(reactElementOrRouterResult, domNode);
} else {
ReactDOM.render(reactElementOrRouterResult, domNode);
}
Expand Down
4 changes: 4 additions & 0 deletions node_package/src/createReactElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ export default function createReactElement({
railsContext,
domNodeId,
trace,
shouldHydrate,
}) {
const { name, component, generatorFunction } = componentObj;

if (trace) {
if (railsContext && railsContext.serverSide) {
console.log(`RENDERED ${name} to dom node with id: ${domNodeId} with railsContext:`,
railsContext);
} else if (shouldHydrate) {
console.log(`HYDRATED ${name} in dom node with id: ${domNodeId} using props, railsContext:`,
props, railsContext);
} else {
console.log(`RENDERED ${name} to dom node with id: ${domNodeId} with props, railsContext:`,
props, railsContext);
Expand Down

0 comments on commit 9e86b75

Please sign in to comment.