Skip to content

Instantly share code, notes, and snippets.

@renatorib
Last active May 31, 2018 19:02
Show Gist options
  • Save renatorib/bb64cff9f1fcefcd5636dc3326d3629c to your computer and use it in GitHub Desktop.
Save renatorib/bb64cff9f1fcefcd5636dc3326d3629c to your computer and use it in GitHub Desktop.
SSR - Render Only In Client
class OnlyInClient extends Component {
static defaultProps = { fallback: null }
state = { client: false }
componentDidMount() {
this.setState({ client: true })
}
render() {
if (!this.state.client) {
return this.props.fallback
}
return this.props.children()
}
}
// nullable
<OnlyInClient>
{() => <Content />}
</OnlyInClient>
// with a fallback
<OnlyInClient fallback={<Spinner />}>
{() => <Content />}
</OnlyInClient>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment