withEffects
Used to enhance a plain component, wrapping it in a WithEffects component which handles side-effects internally. It is a curried higher-order component.
Packages
withEffects
is provided by our React, Inferno or Preact packages - refract-*
, refract-inferno-*
, refract-preact-*
.
Signature
Arguments
aperture
(function): a function which observes data sources within your app, passes this data through any necessary logic flows, and outputs a stream ofeffect
values in response.Signature:
(component, initialProps, initialContext?) => { return effectStream }
.The
component
is an object which lets you observe your React, Inferno or Preact component: see Observing ReactThe
initialProps
are all props passed into theWrappedComponent
.The
initialContext
is the initial context value of the providedContext
(see above, React >= 16.6.0 only)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.
a
config
object optionally containing the following:handler
(function): an optional function which causes side-effects in response toeffect
values.Signature:
(initialProps, initialContext?) => (effect) => { /* handle effect here */ }
The
initialProps
are all props passed into theWrappedComponent
.The
initialContext
is the initial context value of the providedContext
(see below, React >= 16.6.0 only)The
effect
is each value emitted by youraperture
.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 youraperture
. Typically used for logging errors.Signature:
(initialProps, initialContext?) => (error) => { /* handle error here */ }
The
initialProps
are all props passed into theWrappedComponent
.The
initialContext
is the initial context value of the providedContext
(see below, React >= 16.6.0 only)The
error
is each value emitted by youraperture
.Within the body of the function, you cause any side-effect you wish.
Context
(ReactContext): a React Context object. Its initial value will be passed tohandler
,errorHandler
andaperture
(React 16.6.0 and above only).mergeProps
: whether or not props passed withtoProps
orasProps
should be merged together.decorateProps
: whether or not props which are functions should be decorated, so their arguments can be observed (default totrue
).
BaseComponent
(React component): any react component.
Returns
WrappedComponent
(React component): a new component which contains your side-effect logic, and which will render your component as per usual.
Example
Tips
Take a look at our recipe for dependency injection into your Refract components.
withEffects
is curried so that you can re-use a boundhandler
(anderrorHandler
) with multiple differentapertures
.
Last updated