React.Basic.Hooks
- Package
- purescript-react-basic-hooks
- Repository
- spicydonuts/purescript-react-basic-hooks
This is an experimental implementation of React hooks on react-basic.
Warning: This API is experimental and relies on alpha-release React versions. It's here to allow experimentation while we get feedback on the API and wait for an official React release which supports hooks. For more info on hooks, see React's documentation.
It's also recommended while using this module to use PureScript's new "qualified do" syntax (it's used in the examples, React.do).
It's available in the 0.12.2 release.
If we prefer this API over the existing react-basic API, we may eventually replace it with this.
A note on Refs: The Ref type is useful for passing to DOM nodes, but while this module remains a small extension to the existing react-basic library it won't be possible to pass a ref prop to the native DOM components.
In the meantime, use element (unsafeCreateDOMComponent "div") { ref: elementRef }.
#CreateComponent Source
type CreateComponent props = Effect (ReactComponent props)Alias for convenience.
#component Source
component :: forall props hooks. String -> (Record props -> Render Unit hooks JSX) -> CreateComponent (Record props)Create a React component given a display name and render function. Creating components is effectful because React uses the function instance as the component's "identity" or "type". Components should be created during a bootstrap phase and not within component lifecycles or render functions.
#memo Source
memo :: forall props. CreateComponent props -> CreateComponent props#useLayoutEffect Source
useLayoutEffect :: forall key. Eq key => key -> Effect (Effect Unit) -> Hook (UseLayoutEffect key) Unit#useReducer Source
useReducer :: forall action state. state -> (state -> action -> state) -> Hook (UseReducer state action) (Tuple state (action -> Effect Unit))#useContext Source
useContext :: forall a. Context a -> Hook (UseContext a) (Maybe a)#createContext Source
createContext :: forall a. Effect (Context a)#useCallback Source
useCallback :: forall a key. Eq key => key -> a -> Hook (UseCallback key a) a#useEqCache Source
useEqCache :: forall a. Eq a => a -> Hook (UseCallback a a) a#UnsafeReference Source
#Render Source
newtype Render x y aRender represents the effects allowed within a React component's
body, i.e. during "render". This includes hooks and ends with
returning JSX (see pure), but does not allow arbitrary side
effects.
Instances
IxFunctor RenderIxApply RenderIxApplicative RenderIxBind RenderFunctor (Render x y)(TypeEquals x y) => Apply (Render x y)(TypeEquals x y) => Applicative (Render x y)(TypeEquals x y) => Bind (Render x y)(TypeEquals x y, Semigroup a) => Semigroup (Render x y a)(TypeEquals x y, Monoid a) => Monoid (Render x y a)
#displayName Source
displayName :: forall props. ReactComponent props -> StringRetrieve the Display Name from a ReactComponent. Useful for debugging and improving
error messages in logs.
See also: component
Re-exports from Data.Tuple
#Tuple Source
data Tuple a bA simple product type for wrapping a pair of component values.
Constructors
Tuple a b
Instances
(Show a, Show b) => Show (Tuple a b)(Eq a, Eq b) => Eq (Tuple a b)Allows
Tuples to be checked for equality with==and/=whenever there areEqinstances for both component types.(Eq a) => Eq1 (Tuple a)(Ord a, Ord b) => Ord (Tuple a b)Allows
Tuples to be compared withcompare,>,>=,<and<=whenever there areOrdinstances for both component types. To obtain the result, thefsts arecompared, and if they areEQual, thesnds arecompared.(Ord a) => Ord1 (Tuple a)(Bounded a, Bounded b) => Bounded (Tuple a b)Semigroupoid Tuple(Semigroup a, Semigroup b) => Semigroup (Tuple a b)The
Semigroupinstance enables use of the associative operator<>onTuples whenever there areSemigroupinstances for the component types. The<>operator is applied pairwise, so:(Tuple a1 b1) <> (Tuple a2 b2) = Tuple (a1 <> a2) (b1 <> b2)(Monoid a, Monoid b) => Monoid (Tuple a b)(Semiring a, Semiring b) => Semiring (Tuple a b)(Ring a, Ring b) => Ring (Tuple a b)(CommutativeRing a, CommutativeRing b) => CommutativeRing (Tuple a b)(HeytingAlgebra a, HeytingAlgebra b) => HeytingAlgebra (Tuple a b)(BooleanAlgebra a, BooleanAlgebra b) => BooleanAlgebra (Tuple a b)Functor (Tuple a)The
Functorinstance allows functions to transform the contents of aTuplewith the<$>operator, applying the function to the second component, so:f <$> (Tuple x y) = Tuple x (f y)FunctorWithIndex Unit (Tuple a)Invariant (Tuple a)Bifunctor Tuple(Semigroup a) => Apply (Tuple a)The
Functorinstance allows functions to transform the contents of aTuplewith the<*>operator whenever there is aSemigroupinstance for thefstcomponent, so:(Tuple a1 f) <*> (Tuple a2 x) == Tuple (a1 <> a2) (f x)Biapply Tuple(Monoid a) => Applicative (Tuple a)Biapplicative Tuple(Semigroup a) => Bind (Tuple a)(Monoid a) => Monad (Tuple a)Extend (Tuple a)Comonad (Tuple a)(Lazy a, Lazy b) => Lazy (Tuple a b)Foldable (Tuple a)Foldable1 (Tuple a)FoldableWithIndex Unit (Tuple a)Bifoldable TupleTraversable (Tuple a)Traversable1 (Tuple a)TraversableWithIndex Unit (Tuple a)Bitraversable Tuple(TypeEquals a Unit) => Distributive (Tuple a)
Re-exports from Data.Tuple.Nested
#(/\) Source
Operator alias for Data.Tuple.Tuple (right-associative / precedence 6)
Shorthand for constructing n-tuples as nested pairs.
a /\ b /\ c /\ d /\ unit becomes Tuple a (Tuple b (Tuple c (Tuple d unit)))
Re-exports from React.Basic
#ReactComponent Source
data ReactComponent :: Type -> TypeRepresents a traditional React component. Useful for JavaScript interop and FFI. For example:
foreign import ComponentRequiringJSHacks :: ReactComponent { someProp :: String }
See also: element, toReactComponent
#JSX Source
data JSX :: TypeRepresents rendered React VDOM (the result of calling React.createElement
in JavaScript).
JSX is a Monoid:
append- Merge two
JSXnodes usingReact.Fragment.
- Merge two
mempty- The
emptynode; renders nothing.
- The
Hint: Many useful utility functions already exist for Monoids. For example,
guard can be used to conditionally render a subtree of components.
Instances
#elementKeyed Source
elementKeyed :: forall props. ReactComponent (Record props) -> { key :: String | props } -> JSXCreate a JSX node from a ReactComponent, by providing the props and a key.
This function is for non-React-Basic React components, such as those imported from FFI.
See also: ReactComponent, element, React's documentation regarding the special key prop
Allows
Tuples to be rendered as a string withshowwhenever there areShowinstances for both component types.