-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ReferenceError: document is not defined #137
Comments
👍 |
Getting this same issue still in 0.11.. |
I'm not really that well versed in the SSR bit, but shouldn't #113 have fixed this? |
I'm having the same issue... (node ssr) If I require the datepicker on the server without even using it it shows that error. A workaround I found is that if I require it just before I open the picker I don't see it, in my case I will never render the picker on the server side, so it works. I tried to locate the issue and seems to be with tether itself, see: var transformKey = (function () {
var el = document.createElement('div');
var transforms = ['transform', 'webkitTransform', 'OTransform', 'MozTransform', 'msTransform'];
for (var i = 0; i < transforms.length; ++i) {
var key = transforms[i];
if (el.style[key] !== undefined) {
return key;
}
}
})(); That code gets executed when you include tether. Does this have to do anything with the way the datepicker is being bundled with webpack? Sorry I have to do something else so I cant keep digging. If I have time later I'll do it. |
Right, I think I know where this issue is coming from. I was a bit confused, as we clearly only load Tether when I'll have to dig into this, we noticed this @Hacker0x01 when we tried to upgrade and suddenly our underscore was replaced by lodash in the global namespace. |
Has there been any progress on this? I'm still getting "document is not defined" when upgrading to 0.11.4, and have to revert to 0.8.0. |
Getting same error in 0.12. Are there any workarounds? |
Well, I'm still using it, I just made a fugly workaround, fortunately the component that contains the datepicker has to re-render after some data gets loaded into the page so what I did is if I canUseDom I render the datepicker, otherwise I just render a simple input. import { canUseDOM } from 'react/lib/ExecutionEnvironment';
...
dateField = function(label, minDate, date, onChange){
if(canUseDOM){
var DatePicker = loadDatePicker();
return <DatePicker
dateFormat={'MM/DD/YY'}
minDate={minDate}
selected={date}
maxDate={moment().add(1, 'y')}
placeholderText={label}
onChange={onChange}
/>;
}
return <input
type='text'
placeholder={label}
groupClassName='input-group-sm'/>;
},
loadDatePicker = function(){
if(DP == null){
DP = require('react-datepicker');
}
return DP;
};
...
<div className='date-field'>
{dateField('from', moment(), f.getFromDate(), onFromChange)}
</div> |
Just out of curiosity... Do you get any error for inconsistency between Em Qui, 27 de ago de 2015 11:17, Gabriel Espinola notifications@github.com
|
Same error
with
Trace:
|
To anyone interested I created a small wrapper, that you can import instead of the DatePicker. I just import {default as DatePicker} from './IsomorphicDatePicker'; And it seems to work fine. |
Thanks!, It looks good, I like to use
|
I also had this problem when running mocha tests, as the import of Datepicker fails because it tries to access 'document' before jsdom() is called in the test (which is what makes 'document' available). I fixed this by moving the require of Datepicker into the render() method of the component that uses it rather than having it as an import at the top. |
Fixed. |
When I try to server render using Node.js + Express + ReactRouter, I get this:
You are assuming
document
exists, which isn't true when you are server rendering.The most curious part is that I'm copying my example from the server rendering example. This is how my component looks like:
Am I doing anything wrong?
The text was updated successfully, but these errors were encountered: