11

I know how to use redux and react router for SPA. Wonder how to use redux in a more traditional multipage web applications with server side framework such as spring MVC, jsp, etc.

Can multiple pages share the same store? From the documentation for server side rendering, every page server sends needs a new store.

jay.m
  • 1,378
  • 3
  • 13
  • 26
  • I asked a similar question yesterday: http://stackoverflow.com/q/37096921/916450. No real answer (for me), yet.. – Yaniv Efraim May 09 '16 at 07:22
  • The correct answer by kwelch is below. Every page needs an new store instance (which can be initialized with the same data however!) because you lose your whole javascript runtime context (I hope this is the right term), with all loaded libraries etc., when you reload the page. – timotgl Aug 07 '17 at 20:27

2 Answers2

3

If you are using server side rendering (SSR) then the client will be created a new store on each render. A store is created with an initialState, so it is possible to hold state between routes using local/session storage or rendered into the head of a page and used during the store creation to maintain state between different applications.

kwelch
  • 2,172
  • 20
  • 22
0

Glad to hear that you resisted the siren song of a big SPA FOR EVERYTHING that someone are following these days.

Because you're having multi pages, each page is consider as 1 independent React app, so each needs 1 Redux store. However, in many pages of you app, only jQuery or pure React may be enough. So my answer is just simple: use Redux on pages that have complex state management, rich user interaction and high demand of code maintainability.

About the Redux store, you don't need a shared store across pages as you've already have the very good old one that drives the Internet for decades: server session. Just populate this shared value to window.__INITIAL_STATE__ every time the pages are rendered. cookie, local storage, ... are some alternatives but I don't think they are really necessary.

fqxp
  • 6,301
  • 3
  • 21
  • 37
haotang
  • 5,052
  • 30
  • 43
  • I don't think it's good advice to serve a different frontend stack on different pages (as in page A serves react, redux, and jquery, page B serves only react, etc.). The browser caches the js bundle in production anyway and it adds an unnecessary layer of complexity to differentiate between pages. Unless we are really talking about different *applications* on different pages here. – timotgl Aug 22 '17 at 14:21
  • 1
    Dude, we are talking about different page so needless to say about SPA as everyone already knew about it. I still do believe that you can still `jQuery` or even `vanillajs` on simple pages. Use the right tools on the right places. Stop using `react` for everything! – haotang Aug 23 '17 at 00:37