React component to handle client-server render inconsistency
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
/* | |
* 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