2022-11-28

Pass an object from getServerSideProps to page function

In the code below when I return a class object in the function getServerSideProps through props, in the page function the variable is undefined, just like in the code below.

export default function cars(obj){
    return <h1>counter: {obj.counter}</h1> // obj is undefined, why??
}
export async function getServerSideProps({req,res}){
    class Counter{
        constructor(){
            this.counter = 22
        }
    }
    var counter = new Counter()

    return {
        props:
        {
            obj:JSON.stringify(counter)
        }
    }
}

I was expecting that the page parameter obj would have the object counter and not be undefined.



No comments:

Post a Comment