title: Nashorn Error: java.lang.NoSuchMethodException: No such function renderServer
body: <p>I have strange behaviour of Nashorn with React-redux.
  First, I have very simple javascript, which is babelified before deploying (I put here JSX as resulting babelified file is huge):</p>
  
  <pre><code>var React = require('react');
  var ReactDOM = require('react-dom');
  var connect = require('react-redux').connect;
  var Link = require('react-router').Link;
  
  function mapStateToProps(state) {
      console.log("mapStateToProps ", state);
      return {options: state.options};
  };
  
  var IndexContainer = React.createClass({
      render: function () {
          console.log(this.props.options);
          return (
              &lt;div&gt;
                  &lt;Link to="r"&gt;Link!&lt;/Link&gt;
              &lt;/div&gt;
          );
      }
  });
  
  var Index = connect(mapStateToProps)(IndexContainer);
  
  var renderServer = function (data) {
      var data = Java.from(data);
      return React.renderToString(
          React.createElement(Index, {data: data})
      );
  };
  </code></pre>
  
  <p>Second, I have Java code:</p>
  
  <pre><code>@Component
  @SuppressWarnings("restriction")
  public class ReactRenderer {
      private ThreadLocal&lt;NashornScriptEngine&gt; engineHolder = new ThreadLocal&lt;NashornScriptEngine&gt;() {
          @Override
          protected NashornScriptEngine initialValue() {
              NashornScriptEngine nashornScriptEngine = (NashornScriptEngine) new ScriptEngineManager()
                      .getEngineByName("nashorn");
              try {
                  nashornScriptEngine.eval(read("static/nashorn-polyfill.js"));
                  nashornScriptEngine.eval(read("/WEB-INF/resources/js/main.js"));
              } catch (ScriptException e) {
                  throw new RuntimeException(e);
              }
              return nashornScriptEngine;
          } 
      };
  
      private Reader read(String path) {
          InputStream in = getClass().getClassLoader().getResourceAsStream(path);
          return new InputStreamReader(in);
      }
  
      public String render(List&lt;Object&gt; objects) {
          try {
              Object html = engineHolder.get().invokeFunction("renderServer", objects);
              return String.valueOf(html); 
          } catch (Exception e) {
              throw new IllegalStateException("failed to render react component", e);
          }
      }
  }
  </code></pre>
  
  <p>when I invoke render(objects) in java I get:</p>
  
  <pre><code>java.lang.NoSuchMethodException: No such function renderServer
      jdk.nashorn.api.scripting.ScriptObjectMirror.callMember(ScriptObjectMirror.java:204)
      jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:383)
      jdk.nashorn.api.scripting.NashornScriptEngine.invokeFunction(NashornScriptEngine.java:190)
  </code></pre>
  
  <p>if I delete </p>
  
  <pre><code>function mapStateToProps(state) {
      console.log("mapStateToProps ", state);
      return {options: state.options};
  };
  </code></pre>
  
  <p>it does find the renderServer function, but then it fails to eval as no <code>mapStateToProps</code> there, and according to redux <a href="http://redux.js.org/docs/recipes/ServerRendering.html" rel="nofollow">tutorial</a> should be rendered to string the whole thing with all props, <code>Provider</code> etc., not just <code>React component</code>, otherwise handle function using <code>this.store.dispatch</code> will not work.
  What am I doing wrong here and how to make it work?</p>
  
  <hr>
  
  <p>UPDATED:
  I found that the problem is with babelified batch file. If I put to the file I want to evaluate only React components (JS, not JSX) and renderServer function without any libraries, Nashorn is able to find renderServer function and invokes it.
  But all the tutorials say Nashorn is perfect in understanding batch files. 
  So I really don't understand what's the problem could be.</p>