Add ReasonML boilerplate
After some toil, I finally support basic ReasonML starter code. I'm adding it to the nut-score directory because I would like to make a simple webpage that render some nutritional facts about nuts with respect to the ketogenic diet. I'm not sure if I should include or exclude te .bs.js files. See the README.md for more information.
This commit is contained in:
parent
166aed6e80
commit
209fdf5726
14 changed files with 484 additions and 0 deletions
9
nut-score/src/Index.bs.js
Normal file
9
nut-score/src/Index.bs.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
'use strict';
|
||||
|
||||
var React = require("react");
|
||||
var ReactDOMRe = require("reason-react/src/ReactDOMRe.js");
|
||||
var ReducerFromReactJSDocs$ReasonReactExamples = require("./ReducerFromReactJSDocs/ReducerFromReactJSDocs.bs.js");
|
||||
|
||||
ReactDOMRe.renderToElementWithId(React.createElement(ReducerFromReactJSDocs$ReasonReactExamples.make, { }), "mount");
|
||||
|
||||
/* Not a pure module */
|
||||
1
nut-score/src/Index.re
Normal file
1
nut-score/src/Index.re
Normal file
|
|
@ -0,0 +1 @@
|
|||
ReactDOMRe.renderToElementWithId(<ReducerFromReactJSDocs />, "mount");
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
'use strict';
|
||||
|
||||
var Curry = require("bs-platform/lib/js/curry.js");
|
||||
var React = require("react");
|
||||
|
||||
var initialState = /* record */[/* count */0];
|
||||
|
||||
function reducer(state, action) {
|
||||
if (action) {
|
||||
return /* record */[/* count */state[/* count */0] - 1 | 0];
|
||||
} else {
|
||||
return /* record */[/* count */state[/* count */0] + 1 | 0];
|
||||
}
|
||||
}
|
||||
|
||||
function ReducerFromReactJSDocs(Props) {
|
||||
var match = React.useReducer(reducer, initialState);
|
||||
var dispatch = match[1];
|
||||
return React.createElement("div", undefined, React.createElement("div", undefined, "Count: ", String(match[0][/* count */0])), React.createElement("div", undefined, React.createElement("button", {
|
||||
onClick: (function (_event) {
|
||||
return Curry._1(dispatch, /* Decrement */1);
|
||||
})
|
||||
}, "-"), React.createElement("button", {
|
||||
onClick: (function (_event) {
|
||||
return Curry._1(dispatch, /* Increment */0);
|
||||
})
|
||||
}, "+")));
|
||||
}
|
||||
|
||||
var make = ReducerFromReactJSDocs;
|
||||
|
||||
exports.initialState = initialState;
|
||||
exports.reducer = reducer;
|
||||
exports.make = make;
|
||||
/* react Not a pure module */
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
// This is the ReactJS documentation's useReducer example, directly ported over
|
||||
// https://reactjs.org/docs/hooks-reference.html#usereducer
|
||||
|
||||
// Record and variant need explicit declarations.
|
||||
type state = {count: int};
|
||||
|
||||
type action =
|
||||
| Increment
|
||||
| Decrement;
|
||||
|
||||
let initialState = {count: 0};
|
||||
|
||||
let reducer = (state, action) => {
|
||||
switch (action) {
|
||||
| Increment => {count: state.count + 1}
|
||||
| Decrement => {count: state.count - 1}
|
||||
};
|
||||
};
|
||||
|
||||
[@react.component]
|
||||
let make = () => {
|
||||
let (state, dispatch) = React.useReducer(reducer, initialState);
|
||||
|
||||
// We can use a fragment here, but we don't, because we want to style the counter
|
||||
<div>
|
||||
<div>
|
||||
{React.string("Count: ")}
|
||||
{React.string(string_of_int(state.count))}
|
||||
</div>
|
||||
<div>
|
||||
<button onClick={_event => dispatch(Decrement)}>
|
||||
{React.string("-")}
|
||||
</button>
|
||||
<button onClick={_event => dispatch(Increment)}>
|
||||
{React.string("+")}
|
||||
</button>
|
||||
</div>
|
||||
</div>;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue