From c4b9e079bf46b4331623657c50e421287502f36c Mon Sep 17 00:00:00 2001 From: Adam Lane Date: Wed, 2 Jan 2019 02:43:19 -0800 Subject: [PATCH] Add example of getInitialProps to with-reasonml (#5975) The current reasonml needs an example of how to do getInitialProps. tmepple posted the only known way of doing it in [this comment](https://github.com/zeit/next.js/issues/4202#issuecomment-439175214). It is unlikely reasonml users are going to discover that comment or figure it out on their own so having it in an example is critical. Also, dependencies updated. After updating dependencies, I get this error: ``` ModuleBuildError: Module build failed (from ./node_modules/next/dist/build/webpack/loaders/next-babel-loader.js): [1] Error: Cannot find module '@babel/core' [1] babel-loader@8 requires Babel 7.x (the package '@babel/core'). If you'd like to use Babel 6.x ('babel-core'), you should install 'babel-loader@7'. ``` Seems like @babel/core is required as peer dependency per [this comment](https://github.com/babel/gulp-babel/issues/124#issuecomment-326853222) so I added it. The way to do getInitialProps has changed in 7.0.2 canary so this also has a comment in the code about what change is required to get that working. --- examples/with-reasonml/.bsb.lock | 1 + examples/with-reasonml/package.json | 9 +++--- examples/with-reasonml/pages/index.re | 41 +++++++++++++++++++++++---- 3 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 examples/with-reasonml/.bsb.lock diff --git a/examples/with-reasonml/.bsb.lock b/examples/with-reasonml/.bsb.lock new file mode 100644 index 00000000..2fa3cf47 --- /dev/null +++ b/examples/with-reasonml/.bsb.lock @@ -0,0 +1 @@ +16836 \ No newline at end of file diff --git a/examples/with-reasonml/package.json b/examples/with-reasonml/package.json index d2b400bf..a1d8344d 100644 --- a/examples/with-reasonml/package.json +++ b/examples/with-reasonml/package.json @@ -10,12 +10,13 @@ "dependencies": { "babel-plugin-bucklescript": "^0.5.3", "next": "latest", - "react": "^16.4.2", - "react-dom": "^16.4.2", + "react": "^16.7.0", + "react-dom": "^16.7.0", "reason-react": "^0.5.3" }, "devDependencies": { - "bs-platform": "^4.0.5", - "concurrently": "^4.0.1" + "@babel/core": "^7.2.2", + "bs-platform": "^4.0.14", + "concurrently": "^4.1.0" } } diff --git a/examples/with-reasonml/pages/index.re b/examples/with-reasonml/pages/index.re index b87b0efe..f6d82098 100644 --- a/examples/with-reasonml/pages/index.re +++ b/examples/with-reasonml/pages/index.re @@ -1,13 +1,44 @@ let component = ReasonReact.statelessComponent("Index"); -let make = (_children) => { +let make = (~onServer, _children) => { ...component, - render: (_self) => + render: _self =>
-

(ReasonReact.string("HOME PAGE is here!"))

+

+ {ReasonReact.string("HOME PAGE is here!")} +
+ {ReasonReact.string("onServer: " ++ string_of_bool(onServer))} +

-
+ , }; -let default = ReasonReact.wrapReasonForJs(~component, (_jsProps) => make([||])); +let default = ReasonReact.wrapReasonForJs(~component, jsProps => make(~onServer=jsProps##onServer, [||])); + +/* The way to do getInitialProps: https://github.com/zeit/next.js/issues/4202#issuecomment-439175214 */ +let getInitialProps = context => + Js.Promise.make((~resolve, ~reject as _) => { + let onServer = + switch (Js.Nullable.toOption(context##req)) { + | None => false + | Some(_) => true + }; + resolve(. {"onServer": onServer}); + }); + +/* In 7.0.2 canary, you must remove the Js.Promise.make wrapper */ +/* + let getInitialProps = context => { + let onServer = + switch (Js.Nullable.toOption(context##req)) { + | None => false + | Some(_) => true + }; + {"onServer": onServer}; + }; + */ + +let inject = [%bs.raw {| (cls, fn) => cls.getInitialProps = fn |}]; + +inject(default, getInitialProps);