There are multiple frameworks available in the React but we need to use popular and matured one NextJS.
There are multiple reasons to use NextJS
Organize files by feature rather than by type. This makes it easier to manage and scale the project. my-next-app/
├── .next/
├── node_modules/
├── public/
│ ├── images/
│ ├── favicon.ico
│ └── ...
├── src/
│ ├── components/
│ │ ├── Header.js
│ │ └── Footer.js
│ ├── pages/
│ │ ├── api/
│ │ │ └── hello.js
│ │ ├── _app.js
│ │ ├── _document.js
│ │ ├── index.js
│ │ └── about.js
│ ├── styles/
│ │ ├── globals.css
│ │ └── Home.module.css
│ ├── utils/
│ │ └── helper.js
│ ├── hooks/
│ │ └── useAuth.js
│ └── context/
│ └── AuthContext.js
├── .gitignore
├── package.json
├── README.md
└── next.config.js
Project Structure
1. .next/
Automatically generated when you build your project. Do not touch this folder.
2. node_modules/
Contains all the packages installed via the package manager.
3. public/
This directory is used to store static files such as images, fonts, and other assets.
4. src/
A common convention to place your application code here for better organization.
_app.js: Customizes the default App component, allowing you to override it to control page initialization.
_document.js: Customizes the default Document component, useful for augmenting the application's HTML and body tags.
index.js: Represents the homepage of your application.
5. .gitignore
Specifies files and directories that should be ignored by Git.
6. package.json
Contains metadata about the project and its dependencies.
7. README.md
Project documentation.
8. next.config.js
Configuration file for customizing the behavior of the Next.js application.
1. .env Files
Store environment variables in .env.development, .env.stage, and .env.production files.
2. All the .env files are strictly added in the GIT IGNORE file.
3. In NextJS, env variables beginning with NEXT_PUBLIC are available at client side as well. So before naming a variable with NEXT_PUBLIC make sure whether this env variable will be needed on the client side or not.
Break down your application into small, reusable components.
class components are still relevant, especially when specific lifecycle methods or error boundaries are necessary.
As React developers, being comfortable with both functional and class components is essential. While the trend is moving towards functional components, there are still scenarios where class components are the right choice hence you need to use based on your requirements.
If you see that your component is becoming too big try to break it down into smaller components. Keep your components lightweight i.e., they must mostly deal with rendering the view part (JSX / HTML) or the app. Lightweight logic can be kept in the component but try to move complex state logic into a custom hook, even though it may seem that this hook is used only at a single place.
Client side rendering (CSR):
Server side rendering (SSR):
Security Best Practices for routes
1. Keep State as Local as Possible
Minimize the use of global or shared state.
2. When setting and updating state based on the current local state then used setState, useState, useReducer
3. For sharing state across multiple components and to manage complex state, use React's Context API.
4. If you want to introduce global state then you have to use any one from popular state management external libraries like Redux, Zustand, or Recoil.
5. Do not use multiple state management libraries in a project. It will impact the speed and performance.
6. Server-Side State Management
For server-side state management, leverage Next.js's data fetching methods (getServerSideProps, getStaticProps, and getStaticPaths).
1. Consistent Naming Conventions
Use consistent naming for props.
2. Prop Validations
Use TypeScript with using interfaces or type aliases to enforce prop types and validations.
3. Avoid Unnecessary Props
Pass only the props that are necessary. It will increase the speed
4. Use default props for optional props to ensure components always have default values and avoid undefined errors.
Avoid unnecessary re-renders to increase the speed.
Using react-testing-library to write unit tests for each component.
0