Created
May 16, 2018 16:36
-
-
Save slonoed/cfb472bef033c216ebbaa4f3c79c7689 to your computer and use it in GitHub Desktop.
React component to handle client-server render inconsistency
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
/* | |
* ClientRender helps to avoid server-client render inconsistency. | |
* | |
* I.E. client render depends on data from LocalStorage | |
* | |
* Usage: | |
* | |
* r(ClientRender, {server: 'Rendered on server'}, 'Rendered on client') | |
*/ | |
import {Component} from 'react' | |
export default class ClientRender extends Component { | |
state = {loaded: false} | |
componentDidMount() { | |
setTimeout(() => { | |
this.setState({loaded: true}) | |
}, 1) | |
} | |
render() { | |
if (!this.state.loaded) { | |
return this.props.server | |
} | |
return this.props.children | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment