Redux in Nextjs
Unanswered
Maltese posted this in #help-forum
MalteseOP
hello, guys! Can u provide me with resources about how exactly redux work on SPA apps ( with Vite for example, it doesn't matter) and how it works on hybrid apps like those one made with Nextjs. I want to know how exactrly Redux works on both places and why it words like this. I read the Redux docs, but I don't feel confident that I understand what is happening under the hood
16 Replies
What do you mean by on both places?
@Maltese Yeah, next.js pre-render components on the server BUT server is stateless redux has nothing on the server side
@Maltese Yeah, next.js pre-render components on the server BUT server is stateless redux has nothing on the server side
it's just on the client side
MalteseOP
I mean why I need to convert the redux logic so it can work in nextjs. I read the statements they gave in the docs for some of the reasons why we need to convert it, but I want to see an illustration, or some code and some deep explanation how things works under the hood. Not just “we have to do it this way to work”
what illustration did you mean? what I can say for sure is that everything is the same in page router
and in app router, one step is to create a client component which is redux provider
and that's all I guess
MalteseOP
I am just searching for other resources on this topic
and also when you use the slices, it should be a client component
MalteseOP
this is what acemarke says and I think I get the idea, but I want to create more clear definition of what he says, can u help me
Tonkinese
Do you have a specific use case where you think you may need redux?
MalteseOP
I just want to learn using redux with nextjs
Tonkinese
ok, what is the use case?
@Tonkinese ok, what is the use case?
MalteseOP
how the use case matters. I want to know why they created the store with function and call this function in the main layout.tsx
I know what its related to how MPA works, its serves new HTML file for each route and this triggers new instanses of js and this way we will crate new stores for each request and we don't want this.
MalteseOP
in the sense, even if we do not use useRef and makeStore, if we load the store in the root layout, which is requested only once, during the serving of the entry point and during client navigation, this layout is not requested again, only the page is requested. This means that we stay with one layout for the entire life of the application, which means that we can create a store without a ref, because the only time we will get a new layout is when we refresh the browser, which will also reset the store , which is correct behavior
If we create the store into the root layout then there is no reason to make track of it with useRef