簡體   English   中英

預期服務器 HTML 包含匹配<tag>在<tag></tag></tag>

[英]Expected server HTML to contain a matching <tag> in <tag>

我們正在使用 nextjs 並在頁面刷新(或首次加載)時收到此錯誤

我的錯誤是:

react-dom.development.js:88 Warning: Expected server HTML to contain a matching <tag> in <tag>.

我們功能組件的代碼如下所示:

export default MyComponent () {

  if(! props.something){  // ← this is causing the problem.
    return null;
  }

  return (
    <>
     HTML here ...
    </>
  )
}

據我了解,SSR 與客戶端渲染不同,這就是 react 抱怨的原因。

該應用程序運行正常,但此錯誤顯示在控制台中,我們不希望在那里引發很多錯誤,因為這可能會阻止我們在發生真正的錯誤時看到它們。


解決方案:

解決方案是使用動態導入並將組件調用包裝到:

const MyDynamicComponent = dynamic(() => import('./myComponent'), {ssr: false});

//use it:
<MyDynamicComponent />

//OR :

const MyDynamicComponent = dynamic(() => import('./myComponent'))

//use it:
{typeof window !== 'undefined' && (
  <MyDynamicComponent />
)}

可能動態導入您的組件應該可以解決這個問題。 以下是您可以參考的鏈接; https://nextjs.org/docs/advanced-features/dynamic-import

我一直在尋找類似問題的解決方案,但我使用的是 react-window lib,問題似乎就在其中。 我向它傳遞了不同的高度,這取決於它是在服務器上還是在客戶端上加載,所以它給了我不同的元素。

我的解決方案是在開始時傳遞相同的高度,但在useEffect (=客戶端負載)上將其更改為window.innerHeight

當我在 Next 項目中發生這種情況時,我沒有正確嵌套 html 表元素。 具體來說,沒有將我的<tr>包裝在<tbody>

在我的情況下,代碼首先在服務器上執行,然后在瀏覽器中執行,這就是為什么我收到此錯誤以及樣式組件的其他錯誤,例如: Warning: Prop類名did not match. Server: "CardUserInfo.... did not match. Server: "CardUserInfo....

所以這個鏈接幫助: https://github.com/vercel/next.js/discussions/17443在此處輸入鏈接描述,我的解決方案就像在這個討論中實現的 useState useEffect

 const [mount, setMount] = useState(false) useEffect(() => { setMount(true) }, [ ]) return mount&&( <InfoUserCard/>)

在我的情況下,控制台錯誤顯示Warning: Expected server HTML to contain a matching <sup> in <span>. 僅在開發中使用 Safari Web 瀏覽器,即localhost:3000 只需使用 Chrome Web 瀏覽器擺脫這個無聊的錯誤。

使用 Safari Web 瀏覽器在生產中不會存在這個無聊的錯誤。

預期服務器 HTML 包含匹配<div>在<div></div><div id="text_translate"><p>我正在使用 Nextjs 並遇到此錯誤,這導致我的網站出現很多問題。 它使布局混亂。 當我調查我的設備檢測掛鈎導致此問題時。 這是鈎子:</p><pre> const isBrowser = typeof window:== 'undefined' const getMatches = (query: string). boolean =&gt; { if (isBrowser) { return window.matchMedia(query).matches } return false</pre><p> }</p><pre> const useMediaQuery = (range: [number?, any?]): boolean =&gt; { const [minWidth, maxWidth] = range const query = `screen and (min-width: ${minWidth?? 0}px) ${ maxWidth? `and (max-width: ${ typeof maxWidth === 'string'? parseInt(maxWidth, 10): maxWidth }px)`: '' } ` const [shouldMatch, setShouldMatch] = useState(() =&gt; getMatches(query)) useEffect(() =&gt; { let isActive = true const matchMedia = window.matchMedia(query) const updateMatch = () =&gt; { if (isActive) { setShouldMatch(getMatches(query)) } } updateMatch() matchMedia.addEventListener('change', updateMatch) return () =&gt; { matchMedia.removeEventListener('change', updateMatch) isActive = false } }, [query]) return shouldMatch }</pre><p> 誰能幫助我導致此警告的原因?</p><p> 這是錯誤的樣子:<a href="https://i.stack.imgur.com/egLRx.png" rel="nofollow noreferrer">見錯誤</a></p><p>代碼中鈎子的使用</p><pre> const isTablet = useMediaQuery([0, TABLET]) {?isTablet &amp;&amp; ( &lt;Fade shouldShow={isActive}&gt; &lt;img alt={service.?title} className="nox-service-icon" src={service..icon} /&gt; &lt;/Fade&gt;)}</pre></div></div>

[英]Expected server HTML to contain a matching <div> in <div>

Next.js - 預期服務器 HTML 包含匹配<div>在<div></div><div id="text_translate"><p>現場示例可<a href="https://vuter.herbievine.com" rel="nofollow noreferrer">在此處</a>獲得</p><p>我正在嘗試制作一個基本布局,在手機上,只顯示最新的帖子。 在桌面上,左欄應該是帖子,右欄應該是熱門類別和最受歡迎的帖子。</p><p> 這是布局:</p><pre class="lang-js prettyprint-override"> const IndexLayout: React.FC&lt;IndexLayoutProps&gt; = ({}) =&gt; { const cols = useScreenType() return cols === '2-cols'? ( &lt;div className="w-full flex justify-between items-start"&gt; &lt;ListPosts data-comp="ListPosts" className="w-4/6" /&gt; &lt;div className="sticky ml-12 w-2/6 flex flex-col"&gt; &lt;TopCategories data-comp="TopCategories" className="w-full" /&gt; &lt;PopularPosts data-comp="PopularPosts" className="mt-4" /&gt; &lt;/div&gt; &lt;/div&gt; ): ( &lt;ListPosts data-comp="ListPosts" className="w-full" /&gt; ) }</pre><p> 這是useScreenType鈎子:</p><pre class="lang-js prettyprint-override"> import { useMediaQuery } from 'react-responsive' export const useScreenType = () =&gt; { const is2Cols = useMediaQuery({ minWidth: 1300 }) const is1Cols = useMediaQuery({ minWidth: 800 }) if (is2Cols) { return '2-cols' } if (is1Cols) { return '1-cols' } return 'fullscreen' }</pre><p> 我不斷收到這個錯誤:</p><pre> Warning: Expected server HTML to contain a matching &lt;div&gt; in &lt;div&gt;. div ListPosts@webpack-internal:///./components/posts/ListPosts.tsx:31:19 div IndexLayout@webpack-internal:///./components/layout/IndexLayout.tsx:28:149 div Index@webpack-internal:///./pages/index.tsx:24:149 ApolloProvider@webpack-internal:///./node_modules/@apollo/client/react/context/ApolloProvider.js:13:18 s@webpack-internal:///./node_modules/next-apollo/dist/index.es.js:26:1911 div div MyApp@webpack-internal:///./pages/_app.tsx:37:19 ErrorBoundary@webpack-internal:///./node_modules/@next/react-dev-overlay/lib/internal/ErrorBoundary.js:23:47 ReactDevOverlay@webpack-internal:///./node_modules/@next/react-dev-overlay/lib/internal/ReactDevOverlay.js:73:20 Container@webpack-internal:///./node_modules/next/dist/client/index.js:155:20 AppContainer@webpack-internal:///./node_modules/next/dist/client/index.js:643:18 Root@webpack-internal:///./node_modules/next/dist/client/index.js:779:19</pre><p> 現在我認為問題是由於useScreenType鈎子無法獲得寬度,因為window未在服務器上定義。 但是我該如何解決這個問題? 不僅我得到一個錯誤,而且我的 HTML 呈現奇怪。</p><p> 最終的渲染結果是這樣的(當它渲染為“2-cols”時):</p><pre class="lang-html prettyprint-override"> &lt;div class="flex flex-col justify-start items-start w-full"&gt; &lt;div class="mt-6 w-full"&gt;&lt;/div&gt; &lt;div class="mt-4 flex items-center cursor-pointer transform transition hover:scale-105 text-sm"&gt; &lt;div class="w-full p-6 rounded-lg flex flex-col dark:bg-gray-800 shadow-md"&gt;&lt;/div&gt; &lt;div class="mt-4 p-6 rounded-lg flex flex-col dark:bg-gray-800 shadow-md"&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> <strong>注意:</strong>我使用的是 Next.js v10.2.0</p><p> 代碼可以在<a href="https://github.com/herbievine/blog" rel="nofollow noreferrer">GitHub</a>上找到</p></div></div>

[英]Next.js - Expected server HTML to contain a matching <div> in <div>

預期服務器 HTML 包含匹配<div>在<div> . 下一個 JS 錯誤</div><div id="text_translate"><p>我正在使用 next.js 創建一個 whatsapp 克隆。 在第一次加載應用程序時,我收到了這個錯誤。</p><pre> Warning: Expected server HTML to contain a matching &lt;div&gt; in &lt;div&gt;. at div at O (http://localhost:3000/_next/static/chunks/pages/_app.js?ts=1621219033615:37232:19450) at div at Paper (http://localhost:3000/_next/static/chunks/pages/chat.js?ts=1621219033615:45091:23) at WithStyles (http://localhost:3000/_next/static/chunks/pages/chat.js?ts=1621219033615:64751:31) at div at Drawer (http://localhost:3000/_next/static/chunks/pages/chat.js?ts=1621219033615:33839:29) at WithStyles (http://localhost:3000/_next/static/chunks/pages/chat.js?ts=1621219033615:64751:31) at SideBar (http://localhost:3000/_next/static/chunks/pages/chat.js?ts=1621219033615:67329:75) at div at O (http://localhost:3000/_next/static/chunks/pages/_app.js?ts=1621219033615:37232:19450) at Chat (http://localhost:3000/_next/static/chunks/pages/chat.js?ts=1621219033615:73282:70) at SideMenuProvider (http://localhost:3000/_next/static/chunks/pages/_app.js?ts=1621219033615:25916:23) at MyApp (http://localhost:3000/_next/static/chunks/pages/_app.js?ts=1621219033615:31532:24) at ErrorBoundary (http://localhost:3000/_next/static/chunks/main.js?ts=1621219033615:726:47) at ReactDevOverlay (http://localhost:3000/_next/static/chunks/main.js?ts=1621219033615:829:23) at Container (http://localhost:3000/_next/static/chunks/main.js?ts=1621219033615:8388:5) at AppContainer (http://localhost:3000/_next/static/chunks/main.js?ts=1621219033615:8876:24) at Root (http://localhost:3000/_next/static/chunks/main.js?ts=1621219033615:9012:25)</pre><p> 我完全不知道這個錯誤的來源。 他們也沒有指定從哪個文件中發生錯誤。</p><p> 由於此錯誤,App UI 從 APP 的此<a href="https://i.stack.imgur.com/79z45.png" rel="nofollow noreferrer">實際 UI 到 APP</a>的此<a href="https://i.stack.imgur.com/CLMls.png" rel="nofollow noreferrer">錯誤 UI</a></p><p> 如果有人知道為什么會這樣,請幫助我。</p><p> _document.js 代碼在這里</p><pre class="lang-js prettyprint-override">import React from "react"; import Document, { Html, Main, NextScript, Head } from "next/document"; import { ServerStyleSheets } from "@material-ui/core/styles"; export default class MyDocument extends Document { render() { return ( &lt;Html lang="en"&gt; &lt;Head&gt; {/* Meta Tags for SEO */} &lt;meta charSet="utf-8" /&gt; &lt;meta httpEquiv="X-UA-Comatible" content="IE=edge" /&gt; &lt;meta name="description" content="A WhatsApp clone made using next js and firebase." /&gt; &lt;meta name="keywords" content="WhatsApp Clone" /&gt; &lt;/Head&gt; &lt;body&gt; &lt;Main /&gt; &lt;NextScript /&gt; &lt;/body&gt; &lt;/Html&gt; ); } } MyDocument.getInitialProps = async (ctx) =&gt; { const sheets = new ServerStyleSheets(); const originalRenderPage = ctx.renderPage; ctx.renderPage = () =&gt; originalRenderPage({ enhanceApp: (App) =&gt; (props) =&gt; sheets.collect(&lt;App {...props} /&gt;), }); const initialProps = await Document.getInitialProps(ctx); return {...initialProps, styles: [...React.Children.toArray(initialProps.styles), sheets.getStyleElement(), ], }; };</pre></div></div>

[英]Expected server HTML to contain a matching <div> in <div>. Next JS error

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 預期服務器 HTML 包含匹配<div>在<div></div><div id="text_translate"><p>我正在使用 Nextjs 並遇到此錯誤,這導致我的網站出現很多問題。 它使布局混亂。 當我調查我的設備檢測掛鈎導致此問題時。 這是鈎子:</p><pre> const isBrowser = typeof window:== 'undefined' const getMatches = (query: string). boolean =&gt; { if (isBrowser) { return window.matchMedia(query).matches } return false</pre><p> }</p><pre> const useMediaQuery = (range: [number?, any?]): boolean =&gt; { const [minWidth, maxWidth] = range const query = `screen and (min-width: ${minWidth?? 0}px) ${ maxWidth? `and (max-width: ${ typeof maxWidth === 'string'? parseInt(maxWidth, 10): maxWidth }px)`: '' } ` const [shouldMatch, setShouldMatch] = useState(() =&gt; getMatches(query)) useEffect(() =&gt; { let isActive = true const matchMedia = window.matchMedia(query) const updateMatch = () =&gt; { if (isActive) { setShouldMatch(getMatches(query)) } } updateMatch() matchMedia.addEventListener('change', updateMatch) return () =&gt; { matchMedia.removeEventListener('change', updateMatch) isActive = false } }, [query]) return shouldMatch }</pre><p> 誰能幫助我導致此警告的原因?</p><p> 這是錯誤的樣子:<a href="https://i.stack.imgur.com/egLRx.png" rel="nofollow noreferrer">見錯誤</a></p><p>代碼中鈎子的使用</p><pre> const isTablet = useMediaQuery([0, TABLET]) {?isTablet &amp;&amp; ( &lt;Fade shouldShow={isActive}&gt; &lt;img alt={service.?title} className="nox-service-icon" src={service..icon} /&gt; &lt;/Fade&gt;)}</pre></div></div> 警告:預期服務器 HTML 在 div 中包含匹配的導航 Next.js - 預期服務器 HTML 包含匹配<div>在<div></div><div id="text_translate"><p>現場示例可<a href="https://vuter.herbievine.com" rel="nofollow noreferrer">在此處</a>獲得</p><p>我正在嘗試制作一個基本布局,在手機上,只顯示最新的帖子。 在桌面上,左欄應該是帖子,右欄應該是熱門類別和最受歡迎的帖子。</p><p> 這是布局:</p><pre class="lang-js prettyprint-override"> const IndexLayout: React.FC&lt;IndexLayoutProps&gt; = ({}) =&gt; { const cols = useScreenType() return cols === '2-cols'? ( &lt;div className="w-full flex justify-between items-start"&gt; &lt;ListPosts data-comp="ListPosts" className="w-4/6" /&gt; &lt;div className="sticky ml-12 w-2/6 flex flex-col"&gt; &lt;TopCategories data-comp="TopCategories" className="w-full" /&gt; &lt;PopularPosts data-comp="PopularPosts" className="mt-4" /&gt; &lt;/div&gt; &lt;/div&gt; ): ( &lt;ListPosts data-comp="ListPosts" className="w-full" /&gt; ) }</pre><p> 這是useScreenType鈎子:</p><pre class="lang-js prettyprint-override"> import { useMediaQuery } from 'react-responsive' export const useScreenType = () =&gt; { const is2Cols = useMediaQuery({ minWidth: 1300 }) const is1Cols = useMediaQuery({ minWidth: 800 }) if (is2Cols) { return '2-cols' } if (is1Cols) { return '1-cols' } return 'fullscreen' }</pre><p> 我不斷收到這個錯誤:</p><pre> Warning: Expected server HTML to contain a matching &lt;div&gt; in &lt;div&gt;. div ListPosts@webpack-internal:///./components/posts/ListPosts.tsx:31:19 div IndexLayout@webpack-internal:///./components/layout/IndexLayout.tsx:28:149 div Index@webpack-internal:///./pages/index.tsx:24:149 ApolloProvider@webpack-internal:///./node_modules/@apollo/client/react/context/ApolloProvider.js:13:18 s@webpack-internal:///./node_modules/next-apollo/dist/index.es.js:26:1911 div div MyApp@webpack-internal:///./pages/_app.tsx:37:19 ErrorBoundary@webpack-internal:///./node_modules/@next/react-dev-overlay/lib/internal/ErrorBoundary.js:23:47 ReactDevOverlay@webpack-internal:///./node_modules/@next/react-dev-overlay/lib/internal/ReactDevOverlay.js:73:20 Container@webpack-internal:///./node_modules/next/dist/client/index.js:155:20 AppContainer@webpack-internal:///./node_modules/next/dist/client/index.js:643:18 Root@webpack-internal:///./node_modules/next/dist/client/index.js:779:19</pre><p> 現在我認為問題是由於useScreenType鈎子無法獲得寬度,因為window未在服務器上定義。 但是我該如何解決這個問題? 不僅我得到一個錯誤,而且我的 HTML 呈現奇怪。</p><p> 最終的渲染結果是這樣的(當它渲染為“2-cols”時):</p><pre class="lang-html prettyprint-override"> &lt;div class="flex flex-col justify-start items-start w-full"&gt; &lt;div class="mt-6 w-full"&gt;&lt;/div&gt; &lt;div class="mt-4 flex items-center cursor-pointer transform transition hover:scale-105 text-sm"&gt; &lt;div class="w-full p-6 rounded-lg flex flex-col dark:bg-gray-800 shadow-md"&gt;&lt;/div&gt; &lt;div class="mt-4 p-6 rounded-lg flex flex-col dark:bg-gray-800 shadow-md"&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> <strong>注意:</strong>我使用的是 Next.js v10.2.0</p><p> 代碼可以在<a href="https://github.com/herbievine/blog" rel="nofollow noreferrer">GitHub</a>上找到</p></div></div> 預期服務器 HTML 包含匹配<div>在<div> . 下一個 JS 錯誤</div><div id="text_translate"><p>我正在使用 next.js 創建一個 whatsapp 克隆。 在第一次加載應用程序時,我收到了這個錯誤。</p><pre> Warning: Expected server HTML to contain a matching &lt;div&gt; in &lt;div&gt;. at div at O (http://localhost:3000/_next/static/chunks/pages/_app.js?ts=1621219033615:37232:19450) at div at Paper (http://localhost:3000/_next/static/chunks/pages/chat.js?ts=1621219033615:45091:23) at WithStyles (http://localhost:3000/_next/static/chunks/pages/chat.js?ts=1621219033615:64751:31) at div at Drawer (http://localhost:3000/_next/static/chunks/pages/chat.js?ts=1621219033615:33839:29) at WithStyles (http://localhost:3000/_next/static/chunks/pages/chat.js?ts=1621219033615:64751:31) at SideBar (http://localhost:3000/_next/static/chunks/pages/chat.js?ts=1621219033615:67329:75) at div at O (http://localhost:3000/_next/static/chunks/pages/_app.js?ts=1621219033615:37232:19450) at Chat (http://localhost:3000/_next/static/chunks/pages/chat.js?ts=1621219033615:73282:70) at SideMenuProvider (http://localhost:3000/_next/static/chunks/pages/_app.js?ts=1621219033615:25916:23) at MyApp (http://localhost:3000/_next/static/chunks/pages/_app.js?ts=1621219033615:31532:24) at ErrorBoundary (http://localhost:3000/_next/static/chunks/main.js?ts=1621219033615:726:47) at ReactDevOverlay (http://localhost:3000/_next/static/chunks/main.js?ts=1621219033615:829:23) at Container (http://localhost:3000/_next/static/chunks/main.js?ts=1621219033615:8388:5) at AppContainer (http://localhost:3000/_next/static/chunks/main.js?ts=1621219033615:8876:24) at Root (http://localhost:3000/_next/static/chunks/main.js?ts=1621219033615:9012:25)</pre><p> 我完全不知道這個錯誤的來源。 他們也沒有指定從哪個文件中發生錯誤。</p><p> 由於此錯誤,App UI 從 APP 的此<a href="https://i.stack.imgur.com/79z45.png" rel="nofollow noreferrer">實際 UI 到 APP</a>的此<a href="https://i.stack.imgur.com/CLMls.png" rel="nofollow noreferrer">錯誤 UI</a></p><p> 如果有人知道為什么會這樣,請幫助我。</p><p> _document.js 代碼在這里</p><pre class="lang-js prettyprint-override">import React from "react"; import Document, { Html, Main, NextScript, Head } from "next/document"; import { ServerStyleSheets } from "@material-ui/core/styles"; export default class MyDocument extends Document { render() { return ( &lt;Html lang="en"&gt; &lt;Head&gt; {/* Meta Tags for SEO */} &lt;meta charSet="utf-8" /&gt; &lt;meta httpEquiv="X-UA-Comatible" content="IE=edge" /&gt; &lt;meta name="description" content="A WhatsApp clone made using next js and firebase." /&gt; &lt;meta name="keywords" content="WhatsApp Clone" /&gt; &lt;/Head&gt; &lt;body&gt; &lt;Main /&gt; &lt;NextScript /&gt; &lt;/body&gt; &lt;/Html&gt; ); } } MyDocument.getInitialProps = async (ctx) =&gt; { const sheets = new ServerStyleSheets(); const originalRenderPage = ctx.renderPage; ctx.renderPage = () =&gt; originalRenderPage({ enhanceApp: (App) =&gt; (props) =&gt; sheets.collect(&lt;App {...props} /&gt;), }); const initialProps = await Document.getInitialProps(ctx); return {...initialProps, styles: [...React.Children.toArray(initialProps.styles), sheets.getStyleElement(), ], }; };</pre></div></div> 警告:預期的服務器 HTML 包含匹配的<body>在<div> 下一個 js 錯誤“警告:預期的服務器 HTML 包含匹配<button>項</button><div> <button>&quot;</button> 刪除HTML表標簽IF包含某些內容 除去包含 class 的標簽之外的 html 標簽 用於匹配文本的正則表達式僅包含空格和br標簽 匹配HTML中沒有標簽內容的文本
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM