From 836351dccd5aa2cc410596bd675a20232717a1d5 Mon Sep 17 00:00:00 2001 From: Benney Au Date: Wed, 26 Aug 2020 07:46:30 +1000 Subject: [PATCH] docs(hooks): add usage example for hooks (#991) The type signatures for the hooks are quite hard to read. Especially since most parts of it are abstracted away by the brisk-reconciler. This change makes it easier to discover how to use the common hooks. ![image](https://user-images.githubusercontent.com/18226001/90973151-9b1ac500-e562-11ea-8770-5a88dacc5f33.png) --- src/UI_Hooks/Effect.rei | 16 ++++++++++++++++ src/UI_Hooks/Reducer.rei | 11 +++++++++++ src/UI_Hooks/State.rei | 11 +++++++++++ 3 files changed, 38 insertions(+) diff --git a/src/UI_Hooks/Effect.rei b/src/UI_Hooks/Effect.rei index f2184a37e..1aa36c81c 100644 --- a/src/UI_Hooks/Effect.rei +++ b/src/UI_Hooks/Effect.rei @@ -1,5 +1,21 @@ open Revery_UI.React.Hooks; +/** +{2 Description:} + +The Effect Hook lets you perform side effects in function components. + +{2 Usage:} + +{[ +let%hook () = Hooks.effect(OnMount, () => { + // Some side-effect + + // Clean-up function + None; +}); +]} +*/ let effect: ( Effect.condition('a), diff --git a/src/UI_Hooks/Reducer.rei b/src/UI_Hooks/Reducer.rei index 63f601dc2..0409195e0 100644 --- a/src/UI_Hooks/Reducer.rei +++ b/src/UI_Hooks/Reducer.rei @@ -1,5 +1,16 @@ open Revery_UI.React.Hooks; +/** +{2 Description:} + +An alternative to Hooks.state. Accepts a reducer of type (action, state) => newState, and returns the current state paired with a dispatch function. + +{2 Usage:} + +{[ +let%hook (state, dispatch) = Hooks.reducer(~initialState={counter:0}, reduce); +]} +*/ let reducer: (~initialState: 'a, ('b, 'a) => 'a, t(Reducer.t('a) => 'c, 'd)) => (('a, 'b => unit), t('c, 'd)); diff --git a/src/UI_Hooks/State.rei b/src/UI_Hooks/State.rei index ee4b73996..bac947cc5 100644 --- a/src/UI_Hooks/State.rei +++ b/src/UI_Hooks/State.rei @@ -1,4 +1,15 @@ open Revery_UI.React.Hooks; +/** +{2 Description:} + +TODO + +{2 Usage:} + +{[ +let%hook (state, setState) = Hooks.state(0); +]} +*/ let state: ('a, t(State.t('a) => 'b, 'c)) => (('a, ('a => 'a) => unit), t('b, 'c));