Server-side with IIFE
If your .js is using the IIFE style ReactJS fail to load the component on the server depending on how the component is scoped
example using var or window fail
(function (){ var DivBox = React.createElement('div', null, 'Just a div') }())
this will fail
but (function (){ DivBox = React.createElement('div', null, 'Just a div') }()) without var or window will work. why this behavior ?
With your first one, the variable is inside the anonymous function, so it's not accessible outside that function. With your second one, it's implicitly creating a global variable (due to the lack of var) whichi s why it works. I think the ideal syntax for an IIFE is something like this:
(function(global) {
global.DivBox = React.createElement('div', null, 'Just a div');
}(this));
or
(function(global) {
global.DivBox = React.createElement('div', null, 'Just a div');
}((Function('return this'))()));
But I haven't tested whether this works in ReactJS.NET. The Function('return this')() will return the object of the global scope (usually window or global).
Having said that, the ideal end-state is to use CommonJS modules. All modules are wrapped in IIFEs so they don't leak globals, and you explicitly specify what you want to export:
// exampleModule.js
var DivBox = React.createElement('div', null, 'Just a div');
module.exports = {
DivBox: DivBox,
};
Then to use it:
var DivBox = require('./exampleModule').DivBox;
This is currently not supported by ReactJS.NET as support for JS modules in .NET is quite poor at the moment, and no .NET minifiers have support for modules yet. I may look at integration with Webpack in the future.
@Daniel15 what is the technique the ReactJS.NET team use to get React to be found in the react.js file include in the project, maybe we can use that for now.
@dotfelixb the ideal way to resolve this is to add a shim.js then expose your window.foo to global.foo or just foo. Such shim.js only included for server-side render, not sent to client.
UPDATE: When use shims, pay attention to avoid client side and server side conflict. An example is the client render something depends on Cookies, but the server side render is hard to pass Cookies to render engine.
A workaround is to keep render only depends on regular variables, move the Cookies code to ComponetDidMount hook.
The documentation page on Webpack touches on this when using Webpack: http://reactjs.net/guides/webpack.html. Documentation for other module systems or "vanilla" IIFEs can be added in the future if we like. Closing this out. Thanks!