luqin / react-icheck

:radio_button: iCheck components built with React. Highly customizable checkbox, radio buttons and radio group.
https://luqin.github.io/react-icheck
177 stars 40 forks source link

ReferenceError: navigator is not defined #25

Closed ramusus closed 7 years ago

ramusus commented 8 years ago

I've got this error working under react_on_rails app with prerender=True feature.

It's easy to reproduce: import Checkbox in the component, which you are server pre-rendering.

justin808 commented 8 years ago

@ramusus Should this component work for server rendering?

luqin commented 8 years ago

Hi. This component is noly support browser side now. Welcome to pull a PR to support server rendering.

justin808 commented 8 years ago

@ramusus I'm not convinced yet that we should add the change to React on Rails.

ramusus commented 8 years ago

Actually I don't need to use the Checkbox component with server rendering. I use it in admin part, which doesn't require server rendering, but user facing part requires it strictly. For user facing part I have in initializers/react_on_rails.rb

  config.prerender = true

And I disable prerender for admin:

<%= react_component('Admin', prerender: false) %>

And I got this error, when I try to load user facing index page and any other pages, except admin. So this problem is very important for me. Everything works well if I switch prerender off in config.

The problem line in my code:

import {Checkbox} from 'react-icheck';

If I remove it, error disappears. I think I cannot wrap it with try: catch

ramusus commented 8 years ago

I found one workaround. Replace import with require and call in inside render:

  render() {
    const {Checkbox} = require('react-icheck');
    return <Checkbox .../>;
  },

Now it doesn't throw error.

alex-e-leon commented 8 years ago

@luqin I've added a PR for this issue here: https://github.com/luqin/react-icheck/pull/26 Based on my tests, this was the only blocker preventing server-side rendering

ramusus commented 7 years ago

Thanks, now its solved