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>)
Server side components don't execute componentDidMount()
. That's where you should put any purely browser side logic.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.