useRefract
Last updated
Last updated
Used to create a custom hook.
useRefract
is provided by our React packages - refract-*
. It is only available for versions of React supporting hooks (React 16.7.0-alpha.0 and above).
aperture
(function): a function which observes data sources within your app, passes this data through any necessary logic flows, and outputs a stream of effect
values in response.
Signature: (component, initialData) => { return effectStream }
.
The initialData
passed to the hook (second argument).
The component
is an object which lets you observe: see . The observe
method allows you to observe data
passed to your hook.
Within the body of the function, you observe the event source you choose, pipe the events through your stream library of choice, and return a single stream of effects.
data
(object): an object of data (state, props, context) passed to your hook.
handler
(function): a optional function which causes side-effects in response to effect
values.
Signature: (initialData) => (effect) => { /* handle effect here */ }
The initialData
passed to the hook (second argument).
The effect
is each value emitted by your aperture
.
Within the body of the function, you cause any side-effect you wish.
errorHandler
(function): an optional function for catching any unexpected errors thrown within your aperture
. Typically used for logging errors.
Signature: (initialData) => (error) => { /* handle error here */ }
The initialData
passed to the hook (second argument).
The error
is each value emitted by your aperture
.
Within the body of the function, you cause any side-effect you wish.
componentData
(any): Refract hooks have a special built-in effect to push data to your component. Effects emitted by your aperture and wrapped with toRender
will be returned by your hook.
Take a look at our recipe for into your Refract components.