-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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.
- Loading branch information
Showing
2 changed files
with
55 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
## 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^); |