Latest snippets for nextjs
- install the extension
- reload vscode
- snippets will be ready to use
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<main>{children}</main>
</body>
</html>
);
}
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>
<main>{children}</main>
</body>
</html>
);
}
export default function Layout({ children }) {
return <section>{children}</section>;
}
export default function Layout({ children }: { children: React.ReactNode }) {
return <section>{children}</section>;
}
export default function Page() {
return <div></div>;
}
export default function Loading() {
return <div>Loading...</div>;
}
"use client";
import { useEffect } from "react";
export default function Error({ error, reset }) {
useEffect(() => {
console.error(error);
}, [error]);
return (
<div>
<h2>Something went wrong!</h2>
<button onClick={reset}>Try again</button>
</div>
);
}
"use client";
import { useEffect } from "react";
export default function Error({
error,
reset,
}: {
error: Error & { digest?: string };
reset: () => void;
}) {
useEffect(() => {
console.error(error);
}, [error]);
return (
<div>
<h2>Something went wrong!</h2>
<button onClick={reset}>Try again</button>
</div>
);
}
"use client";
export default function GlobalError({ error, reset }) {
return (
<html>
<body>
<h2>Something went wrong!</h2>
<button onClick={reset}>Try again</button>
</body>
</html>
);
}
"use client";
export default function GlobalError({
error,
reset,
}: {
error: Error & { digest?: string };
reset: () => void;
}) {
return (
<html>
<body>
<h2>Something went wrong!</h2>
<button onClick={reset}>Try again</button>
</body>
</html>
);
}
export default function Template({ children }) {
return <div>{children}</div>;
}
export default function Template({ children }: { children: React.ReactNode }) {
return <div>{children}</div>;
}
export default function Test() {
return <div>Enter</div>;
}
export async function getServerSideProps(ctx) {
return {
props: {
data: null,
},
};
}
import { GetServerSideProps } from "next";
const Test = () => {
return <div>Enter</div>;
};
export const getServerSideProps: GetServerSideProps = async (ctx) => {
return {
props: {},
};
};
export default Test;
export default function Test() {
return <div>Enter</div>;
}
export async function getStaticPaths() {
return {
paths: [],
fallback: false,
};
}
export async function getStaticProps(ctx) {
return {
props: {
data: null,
},
};
}
import { GetStaticPaths, GetStaticProps } from "next";
const test = () => {
return <div>Enter</div>;
};
export const getStaticPaths: GetStaticPaths = () => {
return {
paths: [],
fallback: false,
};
};
export const getStaticProps: GetStaticProps = async (ctx) => {
return {
props: {},
};
};
export default test;
export default function test() {
return (
<div>
<h1>Page Not Found</h1>
</div>
);
}
export default function test() {
return (
<div>
<h1>Server Error</h1>
</div>
);
}
export async function getServerSideProps(ctx) {
return {
props: {
data: null,
},
};
}
export const getServerSideProps: GetServerSideProps = async (ctx) => {
return {
props: {},
};
};
export async function getStaticProps(ctx) {
return {
props: {
data: null,
},
};
}
export const getStaticProps: GetStaticProps = (ctx) => {
return {
props: {},
};
};
export async function getStaticPaths() {
return {
paths: [],
fallback: false,
};
}
export const getStaticPaths: GetStaticPaths = () => {
return {
paths: [],
fallback: false,
};
};
<Link href="path">link</Link>
const FileName = () => {
return <div>CONTENT</div>;
};
export default FileName;
export default () => {
return <div>CONTENT</div>;
};
function FileName() {
return <div>CONTENT</div>;
}
export default FileName;
export default function () {
return <div>CONTENT</div>;
}
export const getServerSideProps = async (ctx) => {
return {
props: {
data: null,
},
};
};
export const getServerSideProps: GetServerSideProps = async (ctx) => {
return {
props: {
data: null,
},
};
};
export const getStaticProps = async (ctx) => {
return {
props: {
data: null,
},
};
};
export const getStaticProps: GetStaticProps = async (ctx) => {
return {
props: {
data: null,
},
};
};
export const getStaticPaths = async () => {
return {
paths: [],
fallback: false,
};
};
export const getStaticPaths: GetStaticPaths = async () => {
return {
paths: [],
fallback: false,
};
};
FileName.getInitialProps = async (ctx) => {
return {
${3:data:null}
}
}
export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />;
}
import type { AppProps } from "next/app";
const MyApp = ({ Component, pageProps }: AppProps) => {
return <Component {...pageProps} />;
};
export default MyApp;
import { Html, Head, Main, NextScript } from "next/document";
export default function Document() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
export async function GET(request) {}
export async function GET(request: Request) {}
export async function POST(request) {}
export async function POST(request: Request) {}
export async function PUT(request) {}
export async function PUT(request: Request) {}
export async function PATCH(request) {}
export async function PATCH(request: Request) {}
export async function DELETE(request) {}
export async function DELETE(request: Request) {}
export default function handler(req, res) {
req.statusCode = 200;
}
import { NextApiRequest, NextApiResponse } from "next";
export default function handler(req: NextApiRequest, res: NextApiResponse) {
req.statusCode = 200;
}
const FileName = () => {
return <div>CONTENT</div>;
};
export const getServerSideProps = async (ctx) => {
return {
props: {
data: null,
},
};
};
export default FileName;
import { GetServerSideProps } from "next";
const Test = () => {
return <div>Enter</div>;
};
export const getServerSideProps: GetServerSideProps = async (ctx) => {
return {
props: {
data: null,
},
};
};
export default Test;
function FileName() {
return <div>CONTENT</div>;
}
export async function getServerSideProps(ctx) {
return {
props: {
data: null,
},
};
}
export default FileName;
const FileName = () => {
return <div>CONTENT</div>;
};
export const getStaticProps = async (ctx) => {
return {
props: {
data: null,
},
};
};
export default FileName;
import { GetStaticProps } from "next";
const Test = () => {
return <div>Enter</div>;
};
export const getStaticProps: GetStaticProps = async (ctx) => {
return {
props: {
data: null,
},
};
};
export default Test;
function FileName() {
return <div>CONTENT</div>;
}
export async function getStaticProps(ctx) {
return {
props: {
data: null,
},
};
}
export default FileName;
const FileName = () => {
return <div>CONTENT</div>;
};
export const getStaticPaths = async () => {
return {
paths: [],
fallback: false,
};
};
export const getStaticProps = async (ctx) => {
return {
props: {
data: null,
},
};
};
export default FileName;
import { GetStaticPaths, GetStaticProps } from "next";
const Test = () => {
return <div>Enter</div>;
};
export const getStaticPaths: GetStaticPaths = async () => {
return {
paths: [],
fallback: false,
};
};
export const getStaticProps: GetStaticProps = async (ctx) => {
return {
props: {
data: null,
},
};
};
export default Test;
import Link from "next/link";
import Router from "next/router";
import { useRouter } from "next/router";
import Head from "next/head";
<Link href="path">
<a>Value</a>
</Link>
const router = useRouter();
<Head>
<title>Title</title>
</Head>
<Image src="path" width="width" height="height" alt="alt" />
import { GetServerSideProps } from "next";
const FileName = () => {
return <div>CONTENT</div>;
};
export const getServerSideProps: GetServerSideProps = async (ctx) => {
return {
props: {
data: null,
},
};
};
export default FileName;
import { GetStaticProps } from "next";
const FileName = () => {
return <div>CONTENT</div>;
};
export const getStaticProps: GetStaticProps = async (ctx) => {
return {
props: {
data: null,
},
};
};
export default FileName;
import { NextApiRequest, NextApiResponse } from "next";
export default (req: NextApiRequest, res: NextApiResponse) => {};
import { AppProps } from 'next/app';
const MyApp = ({ Component pageProps }:AppProps) => {
return <Component {...pageProps} />
}
export default MyApp;
import Document, { Html, Head, Main, NextScript } from "next/document";
class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx);
return { ...initialProps };
}
render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
export default MyDocument;
import { GetStaticPaths, GetStaticProps } from "next";
const FileName = () => {
return <div>CONTENT</div>;
};
export const getStaticPaths: GetStaticPaths = async () => {
return {
paths: [],
fallback: false,
};
};
export const getStaticProps: GetStaticProps = async (ctx) => {
return {
props: {
data: null,
},
};
};
export default FileName;