All Projects → lucasconstantino → react-compose-events

lucasconstantino / react-compose-events

Licence: MIT license
A Higher-Order Component factory to attach outside event listeners

Projects that are alternatives of or similar to react-compose-events

reassemble
Fast Library for the Composition of React Higher-Order-Components
Stars: ✭ 67 (+168%)
Mutual labels:  composition, recompose
Transient
A full stack, reactive architecture for general purpose programming. Algebraic and monadically composable primitives for concurrency, parallelism, event handling, transactions, multithreading, Web, and distributed computing with complete de-inversion of control (No callbacks, no blocking, pure state)
Stars: ✭ 617 (+2368%)
Mutual labels:  events, composition
sse
HTML5 Server-Sent-Events for Go
Stars: ✭ 84 (+236%)
Mutual labels:  events
agones-event-broadcaster
Broadcast Agones GameServers and Fleets states to the external world
Stars: ✭ 22 (-12%)
Mutual labels:  events
SocketIOUnity
A Wrapper for socket.io-client-csharp to work with Unity.
Stars: ✭ 69 (+176%)
Mutual labels:  events
site
Sources of euregjug.eu
Stars: ✭ 26 (+4%)
Mutual labels:  events
PSEventViewer
PSEventViewer (Get-Events) is really useful PowerShell wrapper around Get-WinEvent. One of the features you may be interested in is a simple way of getting “hidden” events data
Stars: ✭ 74 (+196%)
Mutual labels:  events
PoShLog
🔩 PoShLog is PowerShell cross-platform logging module. It allows you to log structured event data into console, file and much more places easily. It's built upon great C# logging library Serilog - https://serilog.net/
Stars: ✭ 108 (+332%)
Mutual labels:  events
recurring events
Elixir library for dealing with recurring events
Stars: ✭ 22 (-12%)
Mutual labels:  events
watermill-amqp
AMQP Pub/Sub for the Watermill project.
Stars: ✭ 27 (+8%)
Mutual labels:  events
dom-locky
🙈🙉🙊 - the best way to scope a scroll, or literally any other event.
Stars: ✭ 18 (-28%)
Mutual labels:  events
restate
A redux fractal state library 🕷
Stars: ✭ 55 (+120%)
Mutual labels:  composition
event-emitter
Event Emitter module for Nest framework (node.js) 🦋
Stars: ✭ 102 (+308%)
Mutual labels:  events
SEPA
Get notifications about changes in your SPARQL endpoint.
Stars: ✭ 21 (-16%)
Mutual labels:  events
go-observer
Go package for simplifying channel-based broadcasting of events from multiple publishers to multiple observers
Stars: ✭ 66 (+164%)
Mutual labels:  events
fs2-es
Event sourcing utilities for FS2
Stars: ✭ 75 (+200%)
Mutual labels:  events
public-speaking
🎤 List of presentation, volunteer and initiatives
Stars: ✭ 11 (-56%)
Mutual labels:  events
ngx-event-modifiers
Event Modifiers for Angular Applications https://netbasal.com/implementing-event-modifiers-in-angular-87e1a07969ce
Stars: ✭ 14 (-44%)
Mutual labels:  events
VLCTechHub-site
VLCTechHub site
Stars: ✭ 23 (-8%)
Mutual labels:  events
swift-composable-app-example
Example iOS app built with module composition in mind.
Stars: ✭ 79 (+216%)
Mutual labels:  composition

React Compose Events Build Status

A Higher-Order Component factory to attach outside event listeners

Why?

It is not that common nowadays, but sometimes, when developing for the web, we still need to rely on events that happen on objects out of React application scope; window events, for instance. There are a couple of solutions out there - the most prominent probably being react-event-listener - but none solved this problem in a way such that it would be easy to use with composition libraries such as recompose. react-compose-events does that.

Install

yarn add react-compose-events

Or, good ol' npm install --save react-compose-events

Usage

Simple as can

import { withEvents } from 'react-compose-events'

const MyScrollListeningComponent = () => (
  <p>
    Look at your console!
  </p>
)

export default withEvents(window, {
  scroll: () => console.log('scrolling!')
})(MyScrollListeningComponent)

With recompose friends

Usually you'll need events to fire in a global object, but have them affect the props used on the components. Here goes some example using recompose tools.

import { compose, withState, withHandlers } from 'recompose'
import { withEvents } from 'react-compose-events'

const MyScrollListeningComponent = ({ scrollTop }) => (
  <p>
    Look! Scroll is at { scrollTop }
  </p>
)

export default compose(
  withState('scrollTop', 'setScrollTop', 0),
  withHandlers({
    scroll: ({ setScrollTop }) => e => setScrollTop(window.scrollY)
  }),
  withEvents(window, ({ scroll }) => ({ scroll })),
)(MyScrollListeningComponent)

Notice here that the second argument of withEvents can be either an object mapping event names to handlers, or a function, which will be called with the piping props and should return the map of events. This way you can have event handlers based on passed props - such as handlers created via withHandlers, as the example shows.

Warnings

Server-side rendering

On SSR you might run into trouble when trying to access global objects such as window, which will probably not be availble. For these cases, the first argument of withEvents can also be passed a function, which will be called only when attaching the event listeners, during componentDidMount.

Function implementations as target

If the provided target both is an implementation of the EventTarget interface and has a typeof of function, it will be executed when resolving the target, which might not be intended. To avoid that, you might want to provided the target as a simple function returning the real target.

License

Copyright (c) 2017 Lucas Constantino Silva

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].