React 16: Warning: Expected server HTML to contain a matching
in <body>

前端 未结 8 2291
太阳男子
太阳男子 2020-12-17 08:37

Since upgrading to React 16 I get this error message:

warning.js:33 Warning: Expected server HTML to contain a matching

in .

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-17 09:10

    I got this using material UI by trying to do const searchParams = new URLSearchParams(process.browser ? window.location.search : '') at the top of the react component in my NextJS app with material-ui SnackBar, I was able to remove the error by putting this in a useEffect hook.

    Entire component for reference:

    export default function SnackBarMessage() {
      const [requestLogin, setRequestLogin] = useState(false)
      const handleClose = (event, reason) => {
        if (reason === 'clickaway') {
          return
        }
    
        setRequestLogin(false)
      }
    
      useEffect(() => {
        // had to move into a useEffect hook
        const searchParams = new URLSearchParams(process.browser ? window.location.search : '')
        const hasRequestLogin = Boolean(searchParams.get('requestLogin'))
        if (hasRequestLogin) {
          setRequestLogin(true)
        }
      }, [])
      return (
        <>
          {requestLogin && (
            
              
                Please Log Back In
              
            
          )}
        
      )
    }
    

提交回复
热议问题