Last active
August 29, 2015 14:08
-
-
Save stevoland/e127f5e9854d0862701e to your computer and use it in GitHub Desktop.
shouldRenderForClientMixin
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
var shouldRenderForClientMixin = { | |
componentDidMount: function () { | |
this.setState({ | |
renderForClient: true | |
}) | |
}, | |
shouldRenderForClient: function () { | |
return typeof document !== 'undefined' && this.state && this.state.renderForClient; | |
} | |
}; | |
var Map = React.createClass({ | |
mixins: [shouldRenderForClientMixin], | |
render: function() { | |
if (!this.shouldRenderForClient()) { | |
return <img />; | |
} | |
return <FancyClientSideMap />; | |
} | |
}); |
@stratospark thanks for testing. you're right of course. I'll change it to:
shouldRenderForClient: function () {
return typeof document !== 'undefined' && this.state && this.state.renderForClient;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I needed to do something like this because node was throwing an exception when it couldn't find document. Also, when React runs on the client, it seems to call shouldRenderClient before componentDidMount, so there is no state yet to check, which throws an exception that I need to catch.