ReactJS using cookie when rendering server side

I am working on a web application and I used this starter kit as the basis of my project. The problem is that the cookie is not available on the server side, so I can not use it to render the component on the server.

I use react-cookie to save account data after login

cookie.save('account', accountData)

and get

var account = cookie.load('account')

when i need it. I see that the cookie is saved in the browser and I can use it on the client side after downloading the component, but I need it on the server side for preliminary rendering operations.

So the question is how to achieve this? Or if the cookie does not work, what is the best way to do this?

+4
source share
1 answer

Do you use cookie-parsermiddleware? From the docs react-cookie:

You can also connect it directly using a Node.js request by adding immediately before renderToString:

reactCookie.plugToRequest(req, res);

(cookieParser middleware required)

Express will not analyze cookie requests unless you have installed and installed any middleware to analyze cookies in your Express application.

+3
source

Source: https://habr.com/ru/post/1622180/


All Articles