|
| 1 | +module Stateful = { |
| 2 | + [@react.component] |
| 3 | + let make = (~title, ~initialValue=0, ~children=React.null) => { |
| 4 | + let (value, setValue) = React.useState(() => initialValue); |
| 5 | + let onClick = _ => setValue(value => value + 1); |
| 6 | + |
| 7 | + <section> |
| 8 | + <h3> {React.string(title)} </h3> |
| 9 | + <button key="asdf" onClick> value->React.int </button> |
| 10 | + children |
| 11 | + </section>; |
| 12 | + }; |
| 13 | +}; |
| 14 | + |
| 15 | +module Reducer = { |
| 16 | + type action = |
| 17 | + | Increment |
| 18 | + | Decrement; |
| 19 | + |
| 20 | + [@react.component] |
| 21 | + let make = (~initialValue=0) => { |
| 22 | + let (state, send) = |
| 23 | + React.useReducer( |
| 24 | + (state, action) => |
| 25 | + switch (action) { |
| 26 | + | Increment => state + 1 |
| 27 | + | Decrement => state - 1 |
| 28 | + }, |
| 29 | + initialValue, |
| 30 | + ); |
| 31 | + |
| 32 | + Js.log2("Reducer state", state); |
| 33 | + |
| 34 | + <section> |
| 35 | + <h3> {React.string("React.useReducer")} </h3> |
| 36 | + <main> state->React.int </main> |
| 37 | + <button onClick={_ => send(Increment)}> |
| 38 | + "Increment"->React.string |
| 39 | + </button> |
| 40 | + <button onClick={_ => send(Decrement)}> |
| 41 | + "Decrement"->React.string |
| 42 | + </button> |
| 43 | + </section>; |
| 44 | + }; |
| 45 | +}; |
| 46 | + |
| 47 | +module ReducerWithMapState = { |
| 48 | + type action = |
| 49 | + | Increment |
| 50 | + | Decrement; |
| 51 | + |
| 52 | + [@react.component] |
| 53 | + let make = (~initialValue=0) => { |
| 54 | + let (state, send) = |
| 55 | + React.useReducerWithMapState( |
| 56 | + (state, action) => |
| 57 | + switch (action) { |
| 58 | + | Increment => state + 1 |
| 59 | + | Decrement => state - 1 |
| 60 | + }, |
| 61 | + initialValue, |
| 62 | + initialValue => initialValue + 75, |
| 63 | + ); |
| 64 | + |
| 65 | + Js.log2("ReducerWithMapState state", state); |
| 66 | + |
| 67 | + <section> |
| 68 | + <h3> {React.string("React.useReducerWithMapState")} </h3> |
| 69 | + <main> state->React.int </main> |
| 70 | + <button onClick={_ => send(Increment)}> |
| 71 | + "Increment"->React.string |
| 72 | + </button> |
| 73 | + <button onClick={_ => send(Decrement)}> |
| 74 | + "Decrement"->React.string |
| 75 | + </button> |
| 76 | + </section>; |
| 77 | + }; |
| 78 | +}; |
| 79 | + |
| 80 | +module WithEffect = { |
| 81 | + [@react.component] |
| 82 | + let make = (~value) => { |
| 83 | + React.useEffect1( |
| 84 | + () => { |
| 85 | + Js.log("useEffect"); |
| 86 | + None; |
| 87 | + }, |
| 88 | + [|value|], |
| 89 | + ); |
| 90 | + |
| 91 | + React.string("React.useEffect"); |
| 92 | + }; |
| 93 | +}; |
| 94 | + |
| 95 | +module RerenderOnEachClick = { |
| 96 | + [@react.component] |
| 97 | + let make = (~value=0, ~callback as _) => { |
| 98 | + let (value, setValue) = React.useState(() => value); |
| 99 | + let onClick = _ => |
| 100 | + if (value < 3) { |
| 101 | + Js.log2("Clicked with:", value); |
| 102 | + setValue(value => value + 1); |
| 103 | + } else { |
| 104 | + Js.log("Max value reached, not firing a rerender"); |
| 105 | + setValue(value => value); |
| 106 | + }; |
| 107 | + |
| 108 | + <section> |
| 109 | + <h3> {React.string("RerenderOnEachClick")} </h3> |
| 110 | + <button onClick> <WithEffect value /> </button> |
| 111 | + </section>; |
| 112 | + }; |
| 113 | +}; |
| 114 | + |
| 115 | +module WithLayoutEffect = { |
| 116 | + [@react.component] |
| 117 | + let make = (~value=0, ~callback) => { |
| 118 | + React.useLayoutEffect1( |
| 119 | + () => { |
| 120 | + callback(value); |
| 121 | + Js.log("useLayoutEffect"); |
| 122 | + None; |
| 123 | + }, |
| 124 | + [|value|], |
| 125 | + ); |
| 126 | + |
| 127 | + <section> <h3> {React.string("React.useLayoutEffect")} </h3> </section>; |
| 128 | + }; |
| 129 | +}; |
| 130 | + |
| 131 | +module WithRefAndEffect = { |
| 132 | + [@react.component] |
| 133 | + let make = (~callback) => { |
| 134 | + let myRef = React.useRef(1); |
| 135 | + React.useEffect0(() => { |
| 136 | + myRef.current = myRef.current + 1; |
| 137 | + callback(myRef); |
| 138 | + None; |
| 139 | + }); |
| 140 | + |
| 141 | + <section> |
| 142 | + <h3> {React.string("React.useRef and useEffect")} </h3> |
| 143 | + <main> {React.int(myRef.current)} </main> |
| 144 | + </section>; |
| 145 | + }; |
| 146 | +}; |
| 147 | + |
| 148 | +[@mel.module "react"] |
| 149 | +external useReducer: |
| 150 | + ([@mel.uncurry] (('state, 'action) => 'state), 'state) => |
| 151 | + ('state, 'action => unit) = |
| 152 | + "useReducer"; |
| 153 | + |
| 154 | +module UseReducerNoProblemo = { |
| 155 | + [@react.component] |
| 156 | + let make = () => { |
| 157 | + let reducer = (v, _) => v + 1; |
| 158 | + let (state, send) = useReducer(reducer, 0); |
| 159 | + Js.log("asdfasd"); |
| 160 | + <button onClick={_ => send(0)}> {React.int(state)} </button>; |
| 161 | + }; |
| 162 | +}; |
| 163 | + |
| 164 | +module App = { |
| 165 | + [@react.component] |
| 166 | + let make = (~initialValue) => { |
| 167 | + let value = 99; |
| 168 | + let callback = _number => (); |
| 169 | + |
| 170 | + <main> |
| 171 | + <Stateful title="Stateful" initialValue /> |
| 172 | + <Reducer key="reducer" initialValue /> |
| 173 | + <ReducerWithMapState key="reducer-with-map-state" initialValue /> |
| 174 | + <RerenderOnEachClick key="rerender-on-each-click" value=0 callback /> |
| 175 | + <WithLayoutEffect key="layout-effect" value callback /> |
| 176 | + <WithRefAndEffect key="ref-and-effect" callback /> |
| 177 | + <UseReducerNoProblemo /> |
| 178 | + </main>; |
| 179 | + }; |
| 180 | +}; |
| 181 | + |
| 182 | +switch (ReactDOM.querySelector("#root")) { |
| 183 | +| Some(el) => |
| 184 | + let root = ReactDOM.Client.createRoot(el); |
| 185 | + ReactDOM.Client.render(root, <App initialValue=0 />); |
| 186 | +| None => Js.log("No root element found") |
| 187 | +}; |
0 commit comments