Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is best approach for state management in server side rendering like next.js?

So working on a large scale application where I need to develop a front end part. And we would like it to be SSR for SEO purposes, But when it comes to state management we have redux for that so how we are going to manage it and what should be the server-side and client-side configuration? if possible also share example. And can we define client-side routing like react-router in next.js?

like image 297
Kumud Gupta Avatar asked Apr 10 '20 06:04

Kumud Gupta


People also ask

How is state managed in Nextjs?

One of the most common ways to manage state is with the useState Hook. We will build an application that lets you increase the score by clicking the button. We first imported the useState hook itself, then set the initial state to be 0 . We also provided a setScore function so we can update the score later.

What is the best state management in React?

Which state management is best in React? React's useState is the best option for local state management. If you need a global state solution, the most popular ones are Redux, MobX, and built-in Context API.

Does Nextjs use server-side rendering?

Next. js has two forms of pre-rendering: Static Generation and Server-side Rendering.

How do I keep component state across pages in next js?

In Next. js, the correct way to persist components between page changes is to use the custom App component. It's quite simple. All you have to do is to create the file /pages/_app.


1 Answers

Use the context api which comes out of the box with react. It good enough to handle most use-cases, redux still has its place but not really required if you're just building a crud app.

Example repo here : https://github.com/zeit/next.js/tree/canary/examples/with-context-api

like image 107
Angad Avatar answered Oct 19 '22 09:10

Angad