Binding function withBus
between Preact and Suber.
Usage
Install:
yarn add preact-suber# or npm install preact-suber --save
Usage:
// Create a component that will listen on the 'SHOW_WARNING' channel// It expects 'bus' as a propComponent { super thisstate = warning: null } { // Start listening for events on component mount // When something arrives, set component state to the warning message thisstop = thispropsbus } { // Stop listening on unmount this } { // Show the warning (if present) if !thisstatewarning return null return <blink> thisstatewarning </blink> } // Create a component will can send on the 'SHOW_WARNING' channel// when clicked. It expects 'bus' as a propconst SenderButton = { const onClick = bus return <button onClick= onClick > children </button>} // To automatically pass the bus to these components// we wrap them with 'withBus'const WarningBannerWithBus = const SenderButtonWithBus = // We use these wrapped components just as we// would with the original components// Remember to wrap your app in BusProvider to make the bus available// for all componentsconst bus = preact
API
Components
Functions
BusProvider
Provider component that makes the bus
available for withBus
through the context.
Attributes
bus: Object
A Suber bus. Import and invokecreateBus()
from Suber to create one.
withBus(component)
Returns the wrapped component with the bus
prop injected.
Arguments
component: Component
Preact component. Can be PureComponent or regular Component
Returns a new React Component
Development setup
yarnnpm run dev