fix: render GlobalAudioPlayer only on client side

This commit is contained in:
Hossein Mehrabi 2023-08-21 15:47:27 +03:30
parent 6b4a81ce11
commit 5fcaef4da6
No known key found for this signature in database
GPG Key ID: 45C04964191AFAA1
2 changed files with 16 additions and 2 deletions

View File

@ -1,3 +1,4 @@
import { GlobalAudioPlayer } from '@/components/GlobalAudioPlayer'
import { ProgressBar } from '@/components/ProgressBar/ProgressBar' import { ProgressBar } from '@/components/ProgressBar/ProgressBar'
import { uiConfigs } from '@/configs/ui.configs' import { uiConfigs } from '@/configs/ui.configs'
import { SearchBarProvider } from '@/context/searchbar.context' import { SearchBarProvider } from '@/context/searchbar.context'
@ -8,7 +9,7 @@ import type { AppProps } from 'next/app'
import Head from 'next/head' import Head from 'next/head'
import { ReactNode } from 'react' import { ReactNode } from 'react'
import { LSDThemeProvider } from '../containers/LSDThemeProvider' import { LSDThemeProvider } from '../containers/LSDThemeProvider'
import { GlobalAudioPlayer } from '@/components/GlobalAudioPlayer' import { useHydrated } from '../utils/useHydrated.util'
type NextLayoutComponentType<P = {}> = NextComponentType< type NextLayoutComponentType<P = {}> = NextComponentType<
NextPageContext, NextPageContext,
@ -23,6 +24,8 @@ type AppLayoutProps<P = {}> = AppProps & {
} }
export default function App({ Component, pageProps }: AppLayoutProps) { export default function App({ Component, pageProps }: AppLayoutProps) {
const hydrated = useHydrated()
const getLayout = const getLayout =
Component.getLayout || Component.getLayout ||
((page: ReactNode) => <DefaultLayout>{page}</DefaultLayout>) ((page: ReactNode) => <DefaultLayout>{page}</DefaultLayout>)
@ -95,7 +98,7 @@ export default function App({ Component, pageProps }: AppLayoutProps) {
<SearchBarProvider> <SearchBarProvider>
{getLayout(<Component {...pageProps} />)} {getLayout(<Component {...pageProps} />)}
</SearchBarProvider> </SearchBarProvider>
<GlobalAudioPlayer /> {hydrated && <GlobalAudioPlayer />}
</LSDThemeProvider> </LSDThemeProvider>
) )
} }

View File

@ -0,0 +1,11 @@
import { useEffect, useState } from 'react'
export const useHydrated = () => {
const [value, setValue] = useState(false)
useEffect(() => {
setValue(true)
}, [])
return value
}