rsnext/examples/with-reasonml/components/GlobalCount.re
Nathan Sculli 5c678537d6 [with-reasonml] Show both internal and shared state managment. (#7312)
* Show both internal and shared state managment in example.
* Add a global state custom hook.
2019-05-23 16:46:42 -05:00

27 lines
532 B
ReasonML

/*
## Global Count
This captures the count globally so that it will be persisted across
the `Index` and `About` pages. This replicates the functionality
of the shared-modules example.
*/
type t = ref(int);
type action =
| Increment;
let current = ref(0);
let increment = () => {
current := current^ + 1;
current;
};
let reducer = (_state, action) => {
switch(action) {
| Increment =>
let updated = increment();
updated^
}
};
let useGlobalCount = () => React.useReducer(reducer, current^);