I am having an issue where the data from fetch was pass to a client-side component.
'use server';
const Page = () => {
const data = awaits fetch(dataUrl)
if (!res.ok) {
throw error
}
return (
<>
<Comp1 data={data}> //server-side
<Comp2 data={data}> //client-side
</>
)
}
'use client';
const Comp2 = (data) => {
console.log("data", data)
const [hero, setHero] = useState('')
useEffect(() => {
if (data.hero) {
setHero(`${data.hero} - hero`)
}
}, [data])
return (
<div>
<h1>data.header</h1> // makes them empty
<DataHero hero={hero} /> // makes them empty since first data is empty but wont render again
</div>
)
}
In terminal
"data" undefined
"data" data: {
header: 'Tess'
hero: 'Mordert'
}
I tried the following but failed:
- JSON.parse(JSON.stringify(data))
- make it into server - serverWrapper - client component //props drilling
- useState
- useMemo
- lazy loading - stuck up on loading and wont render anything.
The only thing work was make the client into server but I don't want this since there are still components custom hooks to be include in Comp2 in later requirements and also the results are reverse from console.log.
Result in console.log
"data" data: {
header: 'Tess'
hero: 'Mordert'
}
"data" undefined