Andriya
Andriya

Reputation: 13

How to avoid getting error 'localStorage is not defined' on server in ReactJS Universal App

I was using localStorage in my webApp to store data on client side. But when I've tried to make the app Universal, this causes an issue. How do I solve this issue?

Upvotes: 0

Views: 4500

Answers (1)

Chris Hawkes
Chris Hawkes

Reputation: 12420

If you're isomorphically rendering React on the server than the code accessing local storage must be put in componentDidMount as this will not be called on the serverside.

Upvotes: 1

Related Questions