In React, how do I detect if my component is rendering from the client or the server?

前端 未结 9 1323
夕颜
夕颜 2020-12-29 18:41

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

9条回答
  •  悲哀的现实
    2020-12-29 19:37

    You could also just use the use-ssr React hook

    import useSSR from 'use-ssr'
    
    const App = () => {
      var { isBrowser, isServer } = useSSR()
    
      // Want array destructuring? You can do that too!
      var [isBrowser, isServer] = useSSR()
    
      /*
       * In your browser's chrome devtools console you should see
       * > IS BROWSER: 

提交回复
热议问题