Skip to content

Commit 1b05e76

Browse files
authored
[Bsb] Better ReasonReact template (#2594)
1 parent 067e32d commit 1b05e76

File tree

6 files changed

+68
-12
lines changed

6 files changed

+68
-12
lines changed

jscomp/bsb/templates/react/README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
# ${bsb:name}
22

3-
## Run this project:
3+
## Run Project
44

5-
```
5+
```sh
66
npm install
77
npm start
88
# in another tab
99
npm run webpack
1010
```
11-
After you see the webpack compilation succeed (the `npm run webpack` step), open up the nested html files in `src/*` (**no server needed!**). Then modify whichever file in `src` and refresh the page to see the changes.
11+
After you see the webpack compilation succeed (the `npm run webpack` step), open up `src/index.html` (**no server needed!**). Then modify whichever `.re` file in `src` and refresh the page to see the changes.
1212

1313
**For more elaborate ReasonReact examples**, please see https://github.com/reasonml-community/reason-react-example
1414

15-
## Build this project
15+
## Build for Production
1616

17-
```
17+
```sh
1818
npm run build
19-
npm run webpack:build
19+
npm run webpack:production
2020
```
2121

22+
This will replace the development artifact `build/Index.js` for an optimized version.

jscomp/bsb/templates/react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"clean": "bsb -clean-world",
88
"test": "echo \"Error: no test specified\" && exit 1",
99
"webpack": "webpack -w",
10-
"webpack:build": "NODE_ENV=production webpack"
10+
"webpack:production": "NODE_ENV=production webpack"
1111
},
1212
"keywords": [
1313
"BuckleScript"

jscomp/bsb/templates/react/src/Page.re renamed to jscomp/bsb/templates/react/src/Component1.re

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ let handleClick = (_event, _self) => Js.log("clicked!");
1616
`ReasonReact.element(Page.make(~message="hello", [||]))` */
1717
let make = (~message, _children) => {
1818
...component,
19-
render: (self) =>
20-
<div onClick=(self.handle(handleClick))> (ReasonReact.stringToElement(message)) </div>
19+
render: self =>
20+
<div onClick=(self.handle(handleClick))>
21+
(ReasonReact.stringToElement(message))
22+
</div>,
2123
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* State declaration */
2+
type state = {
3+
count: int,
4+
show: bool,
5+
};
6+
7+
/* Action declaration */
8+
type action =
9+
| Click
10+
| Toggle;
11+
12+
/* Component template declaration.
13+
Needs to be **after** state and action declarations! */
14+
let component = ReasonReact.reducerComponent("Example");
15+
16+
/* greeting and children are props. `children` isn't used, therefore ignored.
17+
We ignore it by prepending it with an underscore */
18+
let make = (~greeting, _children) => {
19+
/* spread the other default fields of component here and override a few */
20+
...component,
21+
22+
initialState: () => {count: 0, show: true},
23+
24+
/* State transitions */
25+
reducer: (action, state) =>
26+
switch (action) {
27+
| Click => ReasonReact.Update({...state, count: state.count + 1})
28+
| Toggle => ReasonReact.Update({...state, show: ! state.show})
29+
},
30+
31+
render: self => {
32+
let message =
33+
"You've clicked this " ++ string_of_int(self.state.count) ++ " times(s)";
34+
<div>
35+
<button onClick=(_event => self.send(Click))>
36+
(ReasonReact.stringToElement(message))
37+
</button>
38+
<button onClick=(_event => self.send(Toggle))>
39+
(ReasonReact.stringToElement("Toggle greeting"))
40+
</button>
41+
(
42+
self.state.show ?
43+
ReasonReact.stringToElement(greeting) : ReasonReact.nullElement
44+
)
45+
</div>;
46+
},
47+
};
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
ReactDOMRe.renderToElementWithId(<Page message="Hello!" />, "index");
1+
ReactDOMRe.renderToElementWithId(<Component1 message="Hello!" />, "index1");
2+
3+
ReactDOMRe.renderToElementWithId(<Component2 greeting="Hello!" />, "index2");

jscomp/bsb/templates/react/src/index.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
5-
<title>Pure Reason Example</title>
5+
<title>ReasonReact Examples</title>
66
</head>
77
<body>
8-
<div id="index"></div>
8+
Component 1:
9+
<div id="index1"></div>
10+
Component 2:
11+
<div id="index2"></div>
12+
913
<script src="../build/Index.js"></script>
1014
</body>
1115
</html>

0 commit comments

Comments
 (0)