Open In App

Functions in Next JS

Next JS is a React-based open-source web development framework created by Vercel. It is used to build production-ready web applications and comes equipped with features like hot code reloading, data fetching utilities, dynamic API routes, and optimized builds. The framework is built upon React, Webpack, and Babel. NextJS is commonly used for creating landing pages and marketing websites, and it also includes inbuilt features to improve webpage search ranking. One of the biggest advantages of using NextJS is that it solves the routing problem that we often encounter while using React.

Functions in Next JS:

Syntax:

export default function function_name() {
return {
// function body with JSX
}
}

Steps to Create the NextJS Applcation:

npx create-next-app app_name  
cd app_name

Project Structure:

Project Structure

The updated dependencies in package.json file will look like:



"dependencies": {
"react": "^18",
"react-dom": "^18",
"next": "14.1.0"
}

Example: Below is the example of NextJS function.




import Head from "next/head";
import { Inter } from "next/font/google";
import styles from "../app/globals.css";
 
const inter = Inter({ subsets: ["latin"] });
 
const List = (props) => (
    <ul>
        {
            props.items.map((item) => (
                <li key={item}>{item}</li>
            ))
        }
    </ul>
);
export default function Home() {
    return (
        <>
            <div>
                <h1>Welcome To Nextjs Functions</h1>
            </div>
            <main className={`${styles.main} ${inter.className}`}>
                <div className="container">
                    <p>
                        List of Functions in NextJS
 
                    </p>
                    <List items={["Data Fetching function",
                        "Cookies function",
                        "Header function",
                        "Routing Function",
                        "redirect function",
                        "revalidatePath function"]} >
                    </List>
                </div>
 
            </main>
        </>
    );
}




h1{
  text-align: center;
}
 
.container {
   
 
  border: 4px dashed green;
  padding: 1.5rem;
   
}
ul {
  margin: 1rem;
  padding: 1rem;
}
li{
  padding-top: 0.5rem;
}
 
 
@media (prefers-color-scheme: dark) {
  html {
    color-scheme: dark;
  }
}

Start your application using the following command.



npm run dev

Output:

Output


Article Tags :