0

I want to detect if the current component is being rendered by ReactDOM or by ReactDOMServer. The classic canUseDom

const canUseDOM = () => Boolean(
  typeof window !== 'undefined'
  && window.document
  && window.document.createElement
)

won't work, because ReactDOMServer can also be used in a broswer context where window is defined. I want to do something like this:

const MyComponent = () =>
  isRenderedByReactDOM()
    ? (<div>I am being rendered by ReactDOM.render</div>)
    : (<div>I am being rendered by ReactDOMServer.renderToString / ReactDOMServer.renderToStaticMarkup</div>)

1 Answer 1

2

Server side components don't execute componentDidMount(). That's where you should put any purely browser side logic.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I eventually solved it by using a context, similar to the redux provider.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.