You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### Difference between rendering methods in React and Next
6
+
7
+
By default, Next JS pre-renders every page in the application. This means that it generates HTML for each page in advance insead of having it all done by client-side JavaScript
8
+
9
+
In a React app, the JavaScript is loaded which then executes to load the HTMl elements onto the DOM. We just have a div tag with Id tag equal to root. Once the javascript for the page is loaded, it will exceute in the browser, create the different DOM nodes and mount them onto the root div element. This process is called Hydration.
10
+
11
+
In a Next app, the pages are pre-rendered automatically by default, meaning the HTML is already generated with the necessary data and then sent to the browser. The JS then loads and makes the page interactive.
12
+
13
+
#### Why pre-render?
14
+
15
+
1. Pre-rendering improves performance
16
+
1. In a react app, you need to wait for the JavaScript to be executed
17
+
2. Perhaps fetch data from an external API and then render the UI
18
+
3. There is a lag time for the user
19
+
4. With a pre-rendered page, the HTMl is already generated and loads faster
20
+
2. Pre-rendering helps with SEO
21
+
1. This isn't as big of an issue with sites that are built behind a login screen, but if you're building a blog or an e-commerce site, then SEO is a concern.
22
+
2. With a react app, if the search engine hits your page, it only sees a div tag with id equal to root.
23
+
3. If search engines hit a pre-rendered page, all the content is present in the source code which will help with crawlers that are indexing the page.
24
+
4. If SEO is of concern for app, then pre-rendering is what you want
Copy file name to clipboardExpand all lines: pages/index.js
+40Lines changed: 40 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,13 @@
1
1
importLinkfrom'next/link'
2
+
import{useRouter}from'next/router'
2
3
3
4
functionHome(){
5
+
constrouter=useRouter()
6
+
7
+
consthandleClick=()=>{
8
+
console.log('Placing your order')
9
+
router.push('/product')
10
+
}
4
11
return(
5
12
<>
6
13
<h1>Home Page</h1>
@@ -10,8 +17,41 @@ function Home() {
10
17
<Linkhref='/product'>
11
18
<a>Products</a>
12
19
</Link>
20
+
<buttononClick={handleClick}>Place Order</button>
13
21
</>
14
22
)
15
23
}
16
24
17
25
exportdefaultHome
26
+
27
+
/*
28
+
29
+
### Routing Summary
30
+
31
+
Next.js uses a file based routing system.
32
+
33
+
- Page Based Routing System - Pages are associated with a route based on their file name.
34
+
35
+
- Nested Routes are a nested folder structure where files will be automatically routed in the same way in the URL.
36
+
37
+
- Dynamic Routes can be created by adding square brackets to a page name.
38
+
39
+
- Catch All Routes - add three dots (like a spread operator) inside square brackets to create a catch all route. They're helpful if you want different URLs for the same page layout or even when you're working with pages where some of the route parameters are optional.
40
+
41
+
To navigate to other routes (pages), we wrap the heading for those pages in a Link (capital 'L') component and have the href point to the corresponding internal page via '/PAGE'
42
+
43
+
Place Order Button: first, we add the button and give it a click handler. Next, we define the handler through a const. Within, the order body, we're assuming the order was placed successfully and log 'placing your order'. Then, the user should be programmatically navigated to the products page.
44
+
45
+
To do this, at the top, we import useRouter from next/router.
46
+
47
+
Within the component, we make const router = useRouter. Within this object, we can access the push method, passing in the route as the argument.
48
+
49
+
In this example, we're navigating to /product. So, we import next/router and define a router const.
50
+
51
+
We're currenly only trying to navigate back to the product argument, but we can navigate to nested routes, dynamic routes, and catch-all routes.
52
+
53
+
If you want to replace the history instead of pushing the route onto the stack, you can use router.replace. The behavior is similar ot the link component with the prop set to True.
54
+
55
+
Remember - you use the 'useRouter' hook's _router.push_ method to navigate programmatically
0 commit comments