Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
17 replies
Shoodey

How to App.getInitialProps in CT3A?

Heyah - Im trying to use jotai to set an atom read from cookie (sidebar state and color scheme) but I have no clue if Im doing it with typesafety and passing the right session prop
const MyApp = ({
  Component,
  pageProps: { session, showSidebar, ...pageProps },
}: AppProps<{ session: Session | null; showSidebar: boolean }>) => {
  const setShowSidebar = useSetAtom(showSidebarAtom);

  useEffect(() => {
    setShowSidebar(showSidebar);
    console.log(showSidebar);
  }, [setShowSidebar, showSidebar]);

  return (
    <SessionProvider session={session}>
      <Component {...pageProps} />
    </SessionProvider>
  );
};

MyApp.getInitialProps = async (appContext: AppContext) => {
  const ctx = appContext.ctx;

  const appProps = await App.getInitialProps(appContext);
  const session = await getSession({ req: ctx.req });

  const showSidebarCookie = getCookie("showSidebar", ctx);
  const showSidebar =
    showSidebarCookie === undefined ? true : showSidebarCookie;

  return {
    ...appProps,
    pageProps: {
      session,
      showSidebar,
    },
  };
};
Was this page helpful?