Last active
June 28, 2019 20:33
-
-
Save lixiaoyan/e341588023881aaa02da90e941b05761 to your computer and use it in GitHub Desktop.
React 16: ReactDOM.hydrate(...)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import ReactDOM from "react-dom"; | |
import { AppContainer } from "react-hot-loader"; | |
import App from "./App"; | |
const render = (hydrate = false) => { | |
const container = document.querySelector("#app"); | |
const element = ( | |
<AppContainer> | |
<App /> | |
</AppContainer> | |
); | |
if (hydrate) { | |
ReactDOM.hydrate(element, container); | |
} else { | |
ReactDOM.render(element, container); | |
} | |
}; | |
render(true); | |
if (module.hot) { | |
module.hot.accept(() => { | |
render(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This helped me a lot today. Thanks!
You can tighten this up a bit:
Might also want to consider the potential for dead code elimination: