I'm building a isomorphic application, but I'm using a third-party component that only renders on the client. So, particularly for this component, I need to only render it when I'm rendering in the client.
How do I detect if I'm at the client or at the server? I'm looking for something like isClient()
or isServer()
.
At the topmost level of the server Element hierarchy, one could add a
ServerContext
such as this:Doing so, it should be possible to read the isServer from the context like this:
Two things that may be relevant:
Many projects use some convention where they set a global SERVER or CLIENT boolean so all your code can switch based off it. In your server bundle, set some global, like in this project
And in your client bundle, set some global client to true, which you can achieve one way with Webpack's DefinePlugin
With the above approach, you could switch based off that variable in willMount, or render, to do one thing on the server, and another on the client.
The second thing that may be helpful here is
componentDidMount
only runs on the client, but not on the server.