diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..36c2bf4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +example/node_modules +example/build diff --git a/ReactJS.php b/ReactJS.php index 7e04267..8877e37 100644 --- a/ReactJS.php +++ b/ReactJS.php @@ -51,10 +51,10 @@ class ReactJS { function __construct($libsrc, $appsrc) { $react = array(); // stubs, react - $react[] = "var console = {warn: function(){}, error: print}"; - $react[] = "var global = global || this, self = self || this, window = window || this"; + $react[] = "var console = {warn: function(){}, error: print};"; + $react[] = "var global = global || this, self = self || this, window = window || this;"; $react[] = $libsrc; - $react[] = "var React = global.React"; + $react[] = "var React = global.React, ReactDOM = global.ReactDOM, ReactDOMServer = global.ReactDOMServer;"; // app's components $react[] = $appsrc; $react[] = ';'; @@ -100,7 +100,7 @@ function setErrorHandler($err) { */ function getMarkup() { $js = sprintf( - "print(React.renderToString(React.createElement(%s, %s)))", + "print(ReactDOMServer.renderToString(React.createElement(%s, %s)))", $this->component, $this->data); @@ -137,7 +137,7 @@ function getJS($where, $return_var = null) { return ($return_var ? "var $return_var = " : "") . sprintf( - "React.render(React.createElement(%s, %s), %s);", + "ReactDOM.render(React.createElement(%s, %s), %s);", $this->component, $this->data, $where diff --git a/example/README.md b/example/README.md new file mode 100644 index 0000000..465c2c4 --- /dev/null +++ b/example/README.md @@ -0,0 +1,44 @@ +# Example + +This example uses npm to build a custom library that will be inserted to be used for rendering React. You may want to do something similar in your own project. + +## Set up + +1. Ensure node and npm are installed +2. `npm install` +3. `npm run make` + +This will create several files in the `build/` directory. `bundle-react.js` is what will be passed into the `ReactJS` constructor as `libsrc`. It exposes 3 globals: `React`, `ReactDOM`, and `ReactDOMServer`. + +## Alternative Approach + +This example works by building a single bundle for React exposing 3 globals. Another approach would be to concatenate the existing 3 browser bundles that React ships with. Each of these exposes the necessary globals. Here's how you might change the existing `example.php` to make this work. + +```diff +@@ -14,9 +14,11 @@ + include '../ReactJS.php'; + + $rjs = new ReactJS( + // location of React's code +- file_get_contents('build/react-bundle.js'), ++ file_get_contents('path/to/react/react.js') . ++ file_get_contents('path/to/react/react-dom.js') . ++ file_get_contents('path/to/react/react-dom-server.js'), + // app code + file_get_contents('build/table.js') + ); + +@@ -46,9 +48,10 @@ $rjs->setComponent('Table', $data); + +