React-id-swiper: navigator is not defined

Created on 27 Jun 2016  路  4Comments  路  Source: kidjp85/react-id-swiper

I am trying to add this in an isomorphic app. When the server-side code is being run, app throws below error as navigator obj is undefined.

node_modules\swiper\dist\js\swiper.js:3627
var ua = navigator.userAgent.toLowerCase();
^
ReferenceError: navigator is not defined.

Could you please help me on how to get rid of this issue.

Most helpful comment

@kidjp85 : initial set of html is created on server-side, event binding is done in client-side later on. For this reason, Swiper module needs to be initialized only inside 'componentDidMount' method. This way we can make sure that swiper code gets executed only in browser and it just works fine.

All 4 comments

One solution I came up with is, requiring 'swiper' inside 'componentDidMount' as requiring at the top looking for navigator object immediately.

componentDidMount: function () {
var Swiper = require('swiper');
this.swiper = Swiper(ReactDOM.findDOMNode(this), objectAssign({}, this.props));
}

@maheshambiga : Swiper is client side library, it will get error when u try to init or render it on server side. There is a similar issue here https://github.com/nolimits4web/Swiper/issues/1541

@kidjp85 : initial set of html is created on server-side, event binding is done in client-side later on. For this reason, Swiper module needs to be initialized only inside 'componentDidMount' method. This way we can make sure that swiper code gets executed only in browser and it just works fine.

It should be fixed by idea from this https://github.com/nolimits4web/Swiper/issues/1541

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yuschick picture yuschick  路  5Comments

mbenjrinija picture mbenjrinija  路  4Comments

mehrdad517 picture mehrdad517  路  4Comments

cweise picture cweise  路  6Comments

sergiokopplin picture sergiokopplin  路  4Comments