Last active
September 20, 2019 15:37
-
-
Save HaNdTriX/d05752a77219681e80298ef6cbb1f0df to your computer and use it in GitHub Desktop.
This file contains hidden or 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' | |
const isServer = typeof window === 'undefined' | |
const Context = React.createContext([]) | |
export const HeadProvider = ({ head, children }) => ( | |
<Context.Provider value={head}> | |
{children} | |
</Context.Provider> | |
) | |
const Head = ({ children }) => { | |
const head = React.useContext(Context) | |
if (isServer) { | |
head.push( | |
...React.Children.toArray(children) | |
) | |
return null | |
} | |
return ReactDOM.createPortal(children, document.head) | |
} | |
export default Head |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Advantages
Usage
Server rendering setup