Introduction
Next.js contains a feature called as Pages that helps us to maintain a clear isolation of the page level components and their layouts.
This helps us in having page and routing implementations separate from the components, helping in isolation of the application layout / presentation with that of the functional components
This segregation helps us in having slim components and isolate the layout / page level actions. Also, since there are pages, the components that comprise a page atleast at the higher level can be easily found out in this system.
Problem Statement
We have been using react.js for building SPA (Single Page Applications) for a while and we experienced that some features like pages and routing are not very comfortable with react.js
Compared with other frameworks, react does not have option to fully perform the routing and having pages. There are times where we have to duplicate the layouts to all components that act like pages in the react application.
These are resulting in lots of code getting redundant and duplicated across various components in react.js application.
Solution
Pages are configured in a Next.js application and the directory structure looks like the one below
Application Root
> components
> pages
> aboutus
> contact
> products
> index.js (this normally is the entry point file in the page, this typically will invoke the list of products through its component)
> [productId].js (this filename contains the route parameter as a placeholder in the filename itself thereby visually expressing that this file needs a URL Query parameter. In our example, this productId is the id of the product and this page will have components that will render the details of a product.)
This file system organization helps us in the following,
1. Clear separation of what goes in a page
2. Visual indication in the file name itself of what a file is responsible for and also what the parameters the component requires
3. The pages are also react.js components, so that there is no extra effort to learn / implement the pages features
I will share sample application on features of Next.js in subsequent blog posts.
Comments
Post a Comment