Basics Flashcards

1
Q

What is Next.js

A

Next.js is a React framework that allows for server-side rendering and provides a set of tools for building web applications. It simplifies the development process and enhances performance by enabling features like server-side rendering, automatic code splitting, and route pre-fetching.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain server-side rendering (SSR) in Next.js.

A

Server-side rendering in Next.js involves rendering React components on the server side instead of the client side. This improves initial page load performance and provides better SEO by delivering fully rendered HTML pages to the client.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the purpose of the pages directory in Next.js?

A

The pages directory in Next.js is a convention that automatically maps files inside it to routes. Each file represents a specific page or route in the application.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does Next.js handle routing?

A

Next.js uses file-based routing. Each file inside the **pages **directory corresponds to a route in the application. For example, a file named about.js in the pages directory will create a route for the “/about” path.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the advantages of using Next.js over a traditional React application?

A

Next.js offers advantages like server-side rendering for improved performance, automatic code splitting for optimized loading, simplified routing, and better SEO support.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain the concept of data fetching in Next.js.

A

Next.js provides various methods for fetching data during server-side rendering or on the client side. These include getStaticProps, getServerSideProps, and getInitialProps for different use cases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the purpose of getStaticProps in Next.js?

A

used to fetch data at build time. It enables pre-rendering of pages with data that doesn’t change frequently, resulting in static HTML files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does Next.js handle code splitting?

A

Next.js automatically performs code splitting by default. Each page in the pages directory becomes a separate chunk, and only the necessary JavaScript is loaded for the requested page.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain the purpose of getServerSideProps in Next.js.

A

used to fetch data on every request, providing server-side rendering with the latest data. It is suitable for pages that require frequently updated data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the role of the _app.js file in Next.js?

A

The _app.js file in the pages directory is used to override the default App component. It allows for customization of the layout and is commonly used for adding global styles or context providers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does Next.js handle CSS styling?

A

Next.js supports various methods for styling, including inline styles, CSS modules, and third-party libraries like Styled Components. It also allows global styles using the styles key in the App component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the purpose of the public directory in Next.js?

A

Dynamic routes in Next.js can be created using square brackets [] in the filename. For example, a file named [id].js will match routes like /1, /2, etc., and the value will be accessible through the id parameter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can you create dynamic routes in Next.js?

A

Dynamic routes in Next.js can be created using square brackets [] in the filename. For example, a file named [id].js will match routes like /1, /2, etc., and the value will be accessible through the id parameter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain the purpose of the _error.js file in Next.js.

A

The _error.js file is used to override the default error handling in Next.js. It allows for customizing error pages for different HTTP status codes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does Next.js handle environment variables?

A

Next.js supports environment variables by prefixing them with NEXT_PUBLIC_ for client-side usage. For server-side usage, they can be accessed directly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the purpose of the **next.config.js **file in Next.js?

A

The next.config.js file is used for custom configuration in Next.js. It allows for modifying default settings, adding plugins, and defining other project-specific configurations.

17
Q

Explain the concept of automatic static optimization in Next.js.

A

Automatic static optimization in Next.js involves the automatic generation of static HTML files for pages that do not have data fetching requirements. This enhances performance by serving pre-rendered static content.

18
Q

How can you implement authentication in Next.js?

A

Authentication in Next.js can be implemented using various methods, such as third-party authentication providers like OAuth, token-based authentication, or serverless functions for handling authentication logic.

19
Q

What is the purpose of the Link component in Next.js?

A

The Link component is used for client-side navigation in Next.js applications. It pre-fetches linked pages for faster navigation and helps in maintaining a good user experience.

20
Q

How can you deploy a Next.js application?

A

Next.js applications can be deployed to various hosting providers like Vercel, Netlify, or traditional cloud platforms. Deployment involves running the next build command to generate a production-ready build and then deploying the output to the chosen hosting platform.

21
Q

What is the purpose of getInitialProps?

A

getInitialProps is a method used in Next.js class components to fetch data on the server side and pass it as props to the component. It is commonly used in pages that need to fetch data before rendering on the server or during the initial load on the client side.

// Example using getInitialProps in a class component
class MyComponent extends React.Component {
  static async getInitialProps() {
    // Fetch data from an API or other source
    const data = await fetchData();

    // Return data as props
    return { data };
  }

  render() {
    // Access data as props
    const { data } = this.props;

    // Render component using fetched data
    return <div>{data}</div>;
  }
}
22
Q

getInitialProps in Next.js how is it different from getStaticProps and getServerSideProps?

A
  • getInitialProps is typically used with class components, while getStaticProps and getServerSideProps are used with functional components or pages.
  • Unlike getStaticProps (for static generation) and getServerSideProps (for server-side rendering), getInitialProps can be used for both scenarios depending on the page component’s context.
  • getStaticProps and getServerSideProps are specific to pages, while getInitialProps can be used in any component, including custom components used within pages.

It’s important to note that getInitialProps is an older method, and with the introduction of React hooks and newer Next.js data fetching methods like getStaticProps and getServerSideProps, its usage is becoming less common. When possible, it’s recommended to use the newer data fetching methods for better performance and code organization.