Unverified Commit 6a273df2 authored by Manoj M S's avatar Manoj M S Committed by GitHub

chore(examples): updated the example of with-react-jss to utlize the App router (#74440)

This PR updates the with-react-jss example to use the App Router. Here
are the changes that have been made:

- Renamed the `pages` folder to the `app` folder.
- Converted the `index.tsx `to `page.tsx` file as part of the App
Router.
- Created `layout.tsx` in app directory, merging functionality from both
`_app.tsx` and `_document.tsx`:
  - Wraps content with `StyledComponentsRegistry`
- Created `app/registry.tsx` for JSS setup:
    - Moved JSS Provider and SheetsRegistry from _document.tsx
    - Moved style cleanup logic from _app.tsx
    - Handles server-side style generation
- Updated the package.json file.

The following actions were performed as part of this PR:

- Ran `pnpm prettier-check` with no issues found.
- Executed the `pnpm check-examples` script.

CC: @samcx

---------
Co-authored-by: 's avatarsamcx <sam@vercel.com>
parent 22854654
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/versions
# testing
/coverage
# next.js
/.next/
/out/
# production
/build
# misc
.DS_Store
*.pem
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# local env files
.env*.local
# vercel
.vercel
# typescript
*.tsbuildinfo
next-env.d.ts
# react-jss example
This example shows how to integrate react-jss and jss in Next.js
The critical styles will be injected into the head when server rendered.
## Deploy your own
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) or preview live with [StackBlitz](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/with-react-jss)
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-react-jss&project-name=with-react-jss&repository-name=with-react-jss)
## How to use
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example:
```bash
npx create-next-app --example with-react-jss with-react-jss-app
```
```bash
yarn create next-app --example with-react-jss with-react-jss-app
```
```bash
pnpm create next-app --example with-react-jss with-react-jss-app
```
Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
{
"private": true,
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-jss": "^10.9.2"
},
"devDependencies": {
"@types/node": "18.7.15",
"@types/react": "18.2.8",
"typescript": "4.8.2"
}
}
import type { AppProps } from "next/app";
import { useEffect } from "react";
export default function App({ Component, pageProps }: AppProps) {
useEffect(() => {
const style = document.getElementById("server-side-styles");
if (style && style.parentNode) {
style.parentNode.removeChild(style);
}
}, []);
return <Component {...pageProps} />;
}
import Document, { DocumentContext } from "next/document";
import { SheetsRegistry, JssProvider, createGenerateId } from "react-jss";
export default class JssDocument extends Document {
static async getInitialProps(ctx: DocumentContext) {
const registry = new SheetsRegistry();
const generateId = createGenerateId();
const originalRenderPage = ctx.renderPage;
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) => (
<JssProvider registry={registry} generateId={generateId}>
<App {...props} />
</JssProvider>
),
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
styles: (
<>
{initialProps.styles}
<style id="server-side-styles">{registry.toString()}</style>
</>
),
};
}
}
import { createUseStyles } from "react-jss";
const useStyles = createUseStyles({
container: {
marginTop: 100,
textAlign: "center",
},
header: {
fontSize: 24,
lineHeight: 1.25,
},
});
export default function IndexPage() {
const classes = useStyles();
return (
<div className={classes.container}>
<h1 className={classes.header}>
Example on how to use react-jss with Next.js
</h1>
</div>
);
}
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"moduleResolution": "node",
"module": "esnext",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment