SSR and 'use client' component
Unanswered
Britannia Petite posted this in #help-forum
Britannia PetiteOP
My homepage is rendered on server. It renders component that must have
Now in that component I need to call function getDictionary and I can not do that since it must be called from server due to cookies.
Htf am I supposed to get translations if I can not call this fn ?
'use client' since it uses state. Still, it IS rendered on the server and I can see in the response content of that component.Now in that component I need to call function getDictionary and I can not do that since it must be called from server due to cookies.
Htf am I supposed to get translations if I can not call this fn ?
import { cookies } from 'next/headers'
import { deepObjectLookup } from '../javascript'
import { APP_LANGUAGES, DEFAULT_ALL_LANGUAGE } from '@/contants/languages'
const dictionaries = {
[APP_LANGUAGES.ba]: () => import('../dictionary/ba.json').then((module) => module.default),
[APP_LANGUAGES.en]: () => import('../dictionary/en.json').then((module) => module.default),
}
export const getDictionary = async (mainTranslationPath = '') => {
const cookieStore = cookies()
const langCookie = cookieStore.get('lang')
const lang = langCookie?.value ?? DEFAULT_ALL_LANGUAGE
const dictionary = await dictionaries[lang]()
return (translationSubpath) => {
const completePath = `${mainTranslationPath}.${translationSubpath}`
return deepObjectLookup(dictionary, completePath)
}
}16 Replies
wrap the component with server component and pass the getDictionary data as props
server component can pass props to client component
Britannia PetiteOP
I know that but it requires passing like, few more times... what if component is deeply nested ?
Is that really the only way ?
Is that really the only way ?
use react context
create provider component as server component it will also work
nextjs is like magic
then access it in client component with useContext
you can make a server action
to get cookies
call it in a useEffect
voila, no more passing props + no extra packages to get cookies
but extra client request 🥹
@vbhv React query
Britannia PetiteOP
What react query ? React Query is almost incompatible with Next13 +
It is compatible but it makes more problems then it solves
It helped me to fetch data in server comp and pass it to client comp.many people still use rq with next13.what problems do you face with rq?