Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

Commit 05a69cd

Browse files
committed
Update example to build & use compiled React bundle, JSX
1 parent 05637db commit 05a69cd

File tree

6 files changed

+58
-17
lines changed

6 files changed

+58
-17
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
example/node_modules
2+
example/build

example/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Example
2+
3+
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.
4+
5+
## Set up
6+
7+
1. Ensure node and npm are installed
8+
2. `npm install`
9+
3. `npm run make`
10+
11+
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`.

example/example.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
include '../ReactJS.php';
1515

1616
$rjs = new ReactJS(
17-
file_get_contents('../../react/build/react.js'), // location of React's code
18-
file_get_contents('table.js')); // app code
17+
file_get_contents('build/react-bundle.js'), // location of React's code
18+
file_get_contents('build/table.js')); // app code
1919

2020
// data to be passed to the component
21-
$data =
21+
$data =
2222
array('data' => array(
2323
array(1, 2, 3),
2424
array(4, 5, 6),
@@ -39,14 +39,14 @@
3939
<!-- css and stuff -->
4040
</head>
4141
<body>
42-
42+
4343
<!-- render server content here -->
44-
<div id="page"><?php echo $rjs->getMarkup(); ?></div>
45-
44+
<div id="page"><?php echo $rjs->getMarkup(); ?></div>
45+
4646
<!-- load react and app code -->
4747
<script src="react/build/react.min.js"></script>
4848
<script src="react/build/table.js"></script>
49-
49+
5050
<script>
5151
// client init/render
5252
// this is a straight echo of the JS because the JS resources
@@ -57,4 +57,3 @@
5757
</script>
5858
</body>
5959
</html>
60-

example/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "react-php-v8js",
3+
"private": true,
4+
"version": "1.0.0",
5+
"scripts": {
6+
"make": "npm run make-dev && npm run make-min && npm run make-table",
7+
"make-dev": "browserify -t [ envify --NODE_ENV development ] src/react-bundle.js > build/react-bundle.js",
8+
"make-min": "browserify -t [ envify --NODE_ENV production ] src/react-bundle.js | uglifyjs > build/react-bundle.min.js",
9+
"make-table": "babel --presets react src/table.js > build/table.js"
10+
},
11+
"dependencies": {
12+
"babel-cli": "^6.1.18",
13+
"babel-preset-react": "^6.1.18",
14+
"browserify": "^12.0.1",
15+
"envify": "^3.4.0",
16+
"react": "^0.14.2",
17+
"react-dom": "^0.14.2",
18+
"uglifyjs": "^2.4.10"
19+
}
20+
}

example/src/react-bundle.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// We know we're using browserify which compiles modules with global exposted
2+
global.React = require('react');
3+
global.ReactDOM = require('react-dom');
4+
global.ReactDOMServer = require('react-dom/server');
Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@
88
*/
99
var Table = React.createClass({
1010
render: function () {
11+
var rows = this.props.data.map(function (row) {
12+
var cells = row.map(function(cell) {
13+
return <td>{cell}</td>;
14+
});
15+
16+
return <tr>{cells}</tr>;
17+
});
18+
1119
return (
12-
React.DOM.table(null, React.DOM.tbody(null,
13-
this.props.data.map(function (row) {
14-
return (
15-
React.DOM.tr(null,
16-
row.map(function (cell) {
17-
return React.DOM.td(null, cell);
18-
})));
19-
}))));
20-
}});
20+
<table>
21+
<tbody>{rows}</tbody>
22+
</table>
23+
);
24+
}
25+
});

0 commit comments

Comments
 (0)