Next.js: [ERROR][NEED HELP] Use getServerSideProps with Wrapper

Created on 24 Mar 2020  路  2Comments  路  Source: vercel/next.js

The example with-cookie-example uses getInitialProps, how to use getServerSideProps in that case?

// /components/withAuth.js
export const withAuthSync = WrappedComponent => {
  const Wrapper = props => {
    return <WrappedComponent {...props} />
  }

  Wrapper.getInitialProps = async ctx => {
    const token = auth(ctx)

    const componentProps =
      WrappedComponent.getInitialProps &&
      (await WrappedComponent.getInitialProps(ctx))

    return { ...componentProps, token }
  }

  return Wrapper
}

My page with this wrapper

// /pages/home.js
import React from 'react';
import {withAuthSync} from '../containers/hocs/withAuth';

const Home = (props) => {
    return (
        <div>
            Home Page
        </div>
    );
};

export async function getServerSideProps(ctx) {
    const data = await doSomething();
    return {
        props: {...data}
    };
};

export default withAuthSync(Home);

I have this error

You can not use getInitialProps with getServerSideProps. Please remove getInitialProps. /home

All 2 comments

What's with that title? 馃

Please stop double posting and ignoring the issue template.

Duplicate of https://github.com/zeit/next.js/discussions/11317

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timneutkens picture timneutkens  路  3Comments

YarivGilad picture YarivGilad  路  3Comments

lixiaoyan picture lixiaoyan  路  3Comments

renatorib picture renatorib  路  3Comments

formula349 picture formula349  路  3Comments