React JS Server side issue - window not found

Jan Omacka picture Jan Omacka · Aug 15, 2016 · Viewed 45.4k times · Source

Hi I'm trying to use react-rte in my reactJS project. I have server side rendering and every time I want to use this package I get:

return /msie [6-9]\b/.test(window.navigator.userAgent.toLowerCase());
                               ^
ReferenceError: window is not defined

I guess the problem might be with isomorphic-tools but I don't know how to defer importing package to the client where window is going to be defined already.

Answer

Natrezim picture Natrezim · Oct 24, 2017

Here is a npm library which can handle window, document and global object for you: Global.

Then you can safely write:

import window from 'global'

const mySpecialWindowFunction = () => {
    return /msie [6-9]\b/.test(window.navigator.userAgent.toLowerCase());
};