I am trying to figure out how to pass a variable declared on the server side of my nextjs 13 app into a client side function that I have declared in one of my component files. As you can see in my code I am trying to pass the 'x' variable into the Props function which is part of the client side component. I am trying to console log the variable passed into the Props function to see if the variable is passed correctly. but it is returning as 'undefined'. Can someone explain to me how I can pass this variable? Note: I know in this current example it is not necessary for my component to be on the client side. I am just providing this code as an example to answer my question.
Server-side code:
import Props from "./Components/props"
export default function Home() {
const x = 'hello'
return (
<main>
<Props data = {x} />
<p>Hello</p>
</main>
)
}
Client-side code:
"use client"
export default function Props({ data }) {
console.log(data);
return (
<p>Test</p>
);
};
Error:
error - SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
'hello'
is a string which is serializable. Have you tried directly inserting the string, as indata={'hello'}
? – Staphylorrhaphy