Skip to content

Commit

Permalink
[Bsb] Better ReasonReact template
Browse files Browse the repository at this point in the history
  • Loading branch information
chenglou committed Mar 8, 2018
1 parent 067e32d commit b257193
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 12 deletions.
13 changes: 7 additions & 6 deletions jscomp/bsb/templates/react/README.md
@@ -1,21 +1,22 @@
# ${bsb:name}

## Run this project:
## Run Project

```
```sh
npm install
npm start
# in another tab
npm run webpack
```
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.
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.

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

## Build this project
## Build for Production

```
```sh
npm run build
npm run webpack:build
npm run webpack:production
```

This will replace the development artifact `build/Index.js` for an optimized version.
2 changes: 1 addition & 1 deletion jscomp/bsb/templates/react/package.json
Expand Up @@ -7,7 +7,7 @@
"clean": "bsb -clean-world",
"test": "echo \"Error: no test specified\" && exit 1",
"webpack": "webpack -w",
"webpack:build": "NODE_ENV=production webpack"
"webpack:production": "NODE_ENV=production webpack"
},
"keywords": [
"BuckleScript"
Expand Down
Expand Up @@ -16,6 +16,8 @@ let handleClick = (_event, _self) => Js.log("clicked!");
`ReasonReact.element(Page.make(~message="hello", [||]))` */
let make = (~message, _children) => {
...component,
render: (self) =>
<div onClick=(self.handle(handleClick))> (ReasonReact.stringToElement(message)) </div>
render: self =>
<div onClick=(self.handle(handleClick))>
(ReasonReact.stringToElement(message))
</div>,
};
47 changes: 47 additions & 0 deletions jscomp/bsb/templates/react/src/Component2.re
@@ -0,0 +1,47 @@
/* State declaration */
type state = {
count: int,
show: bool,
};

/* Action declaration */
type action =
| Click
| Toggle;

/* Component template declaration.
Needs to be **after** state and action declarations! */
let component = ReasonReact.reducerComponent("Example");

/* greeting and children are props. `children` isn't used, therefore ignored.
We ignore it by prepending it with an underscore */
let make = (~greeting, _children) => {
/* spread the other default fields of component here and override a few */
...component,

initialState: () => {count: 0, show: true},

/* State transitions */
reducer: (action, state) =>
switch (action) {
| Click => ReasonReact.Update({...state, count: state.count + 1})
| Toggle => ReasonReact.Update({...state, show: ! state.show})
},

render: self => {
let message =
"You've clicked this " ++ string_of_int(self.state.count) ++ " times(s)";
<div>
<button onClick=(_event => self.send(Click))>
(ReasonReact.stringToElement(message))
</button>
<button onClick=(_event => self.send(Toggle))>
(ReasonReact.stringToElement("Toggle greeting"))
</button>
(
self.state.show ?
ReasonReact.stringToElement(greeting) : ReasonReact.nullElement
)
</div>;
},
};
4 changes: 3 additions & 1 deletion jscomp/bsb/templates/react/src/Index.re
@@ -1 +1,3 @@
ReactDOMRe.renderToElementWithId(<Page message="Hello!" />, "index");
ReactDOMRe.renderToElementWithId(<Component1 message="Hello!" />, "index1");

ReactDOMRe.renderToElementWithId(<Component2 greeting="Hello!" />, "index2");
8 changes: 6 additions & 2 deletions jscomp/bsb/templates/react/src/index.html
Expand Up @@ -2,10 +2,14 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Pure Reason Example</title>
<title>ReasonReact Example2</title>
</head>
<body>
<div id="index"></div>
Component 1:
<div id="index1"></div>
Component 2:
<div id="index2"></div>

<script src="../build/Index.js"></script>
</body>
</html>

0 comments on commit b257193

Please sign in to comment.