shadaj / slinky

Write Scala.js React apps just like you would in ES6
https://slinky.dev
MIT License
654 stars 57 forks source link

GetDerivedStateFromProps javscript not generated when using fullOptJS? #231

Closed evbo closed 5 years ago

evbo commented 5 years ago

When using fastOptJS my code runs GetDerivedStateFromProps just fine

But if I only change from fastOptJS to using fullOptJS, suddenly GetDerivedStateFromProps is never called or does not exist.

To verify there wasn't some webpack config that was somehow compromising my code, I set the following to be exactly the same in both cases (no other setting in my project discriminates by optimization mode):

webpackConfigFile in fastOptJS := Some(resourceDirectory.in(Compile).value / "webpack" /  "webpack-fastopt.config.js"),
webpackConfigFile in fullOptJS := Some(resourceDirectory.in(Compile).value / "webpack" /  "webpack-fastopt.config.js"),

Looking at fastOptJS javascript, I have GetDerivedStateFromProps javascript functions clearly defined:

getDerivedStateFromProps__LSearch$Props__LSearch$State__LSearch$State = (function(nextProps, prevState) {
  var x = ((("DERIVED Called!: " + nextProps.Context$1.criteria$1.b$1.mkString__T__T(",")) + ", ") + prevState.criteria$1.b$1.mkString__T__T(","));
  var this$2 = $m_s_Console$();
  var this$3 = $as_Ljava_io_PrintStream(this$2.outVar$2.v$1);
  this$3.java$lang$JSConsoleBasedPrintStream$$printString__T__V((x + "\n"));
  var x$1 = nextProps.Context$1.criteria$1;
  var x$2 = prevState.criteria$1;
  if ((!((x$1 === null) ? (x$2 === null) : x$1.equals__O__Z(x$2)))) {
    var this$5 = $m_s_Console$();
    var this$6 = $as_Ljava_io_PrintStream(this$5.outVar$2.v$1);
    this$6.java$lang$JSConsoleBasedPrintStream$$printString__T__V("FALSE!\n");
    var x$2$2 = nextProps.Context$1.criteria$1;
    var x$3 = prevState.as$1;
    var x$4 = prevState.b$1;
    return new $c_LSearch$State().init___Z__sjs_js_Any__sjs_js_Any__Lsection$Variables(false, x$3, x$4, x$2$2)
  } else {
    var this$8 = $m_s_Console$();
    var this$9 = $as_Ljava_io_PrintStream(this$8.outVar$2.v$1);
    this$9.java$lang$JSConsoleBasedPrintStream$$printString__T__V("NO CHANGE\n");
    return prevState
  }
});

Looking at fullOptJS javascript, these are the only matches in the whole file:

a.getDerivedStateFromProps__O__O__O,jg().vs)||(e.getDerivedStateFromProps=function(a,b,c,e){return function(f,q){f=\"object\"===typeof f&&f.hasOwnProperty(\"__\")?f.__:kg(lg(),b,f);q=\"object\"===typeof q&&q.hasOwnProperty(\"__\")?q.__:kg(lg(),c,q);q=a.ht(f,q);return jg().lg?mg(lg(),e,q):{__:q}}}(a,null,c,b));C(D(),a.getDerivedStateFromError__sjs_js_Error__O,jg().us)||(e.getDerivedStateFromError=function(a,b){return function(){return jg().lg?\nmg(lg(),b,null):{__:null}}}(a,b));null!==c&&(a._stateReader=c);null!==b&&(a._stateWriter=b);return ng(jg().Rv,e,a)}cg.prototype.Yk=function(a,b){this.mt=a;this.nt=b;this.nm=null;return this};function og(){this.Rv=this.us=this.vs=null;this.lg=!1}og.prototype=new w;og.prototype.constructor=og;\nog.prototype.a=function(){pg=this;this.vs=(new qg).a().getDerivedStateFromProps__O__O__O;this.us=(new rg).a().getDerivedStateFromError__sjs_js_Error__O;

I'm using webpack with relevant dependencies:

version in webpack := "4.28.3",
version in startWebpackDevServer:= "3.1.14",
scalaJSUseMainModuleInitializer := true,
scalaJSModuleKind := ModuleKind.CommonJSModule,
scalacOptions += "-P:scalajs:sjsDefinedByDefault",

val slinkyVersion = "0.5.2"
libraryDependencies ++= Seq(
      "me.shadaj" %%% "slinky-core" % slinkyVersion,
      "me.shadaj" %%% "slinky-web" % slinkyVersion,
      "me.shadaj" %%% "slinky-native" % slinkyVersion,
      "me.shadaj" %%% "slinky-hot" % slinkyVersion,
      "me.shadaj" %%% "slinky-readwrite" % slinkyVersion,
      "com.apollographql" %%% "apollo-scalajs-react" % "0.6.0"
    )
evbo commented 5 years ago

I'm not having this issue when using componentWillReceiveProps, so as I workaround I can use that method for now just fine thankfully!

shadaj commented 5 years ago

Hi @evbo, this is definitely a bug. Thanks for the detailed report!