Im using "react": "~0.12.2"
And the following code:
@RequestMapping("/")
@ResponseBody
private String home() throws ScriptException, IOException {
loadReact();
InputStream jsStream = resourceLoader.getResource("classpath:public/js/server.js").getInputStream();
engine.eval(new InputStreamReader(jsStream));
return engine.eval("renderServerside()").toString();
}
private void loadReact() throws ScriptException, IOException {
InputStream jsStream = resourceLoader.getResource("classpath:public/lib/react/react.js").getInputStream();
// React expects 'window' or 'global' to be set
engine.eval("var global = this");
engine.eval(new InputStreamReader(jsStream));
}
My server.js looks like this:
function renderServerside() {
var MyComponent = React.createClass({
render: function () {
return React.DOM.h1(null, 'Hi, ' + this.props.msg)
}
});
return React.renderComponentToString(MyComponent({msg: 'World!'}));
}
And getting this exception:
javax.script.ScriptException: ReferenceError: "console" is not defined in <eval> at line number 18108] with root cause
jdk.nashorn.internal.runtime.ECMAException: ReferenceError: "console" is not defined
So how can nashorn not provide a console?
I tired to implement the basic example from this page: React with Nashorn
Rhino before it didn't provide one either. You can implement your own console with something like
console = {
log: print,
warn: print,
error: print
};
For full blown console support you might need to add more functions. See: Console