All Projects → labcodes → rel-events

labcodes / rel-events

Licence: MIT license
The relevant React Events Library.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to rel-events

dom-locky
🙈🙉🙊 - the best way to scope a scroll, or literally any other event.
Stars: ✭ 18 (-10%)
Mutual labels:  events
recurring events
Elixir library for dealing with recurring events
Stars: ✭ 22 (+10%)
Mutual labels:  events
MojoJS-Query
A pure javascript CSS Selector engine.
Stars: ✭ 22 (+10%)
Mutual labels:  javascript-library
fs2-es
Event sourcing utilities for FS2
Stars: ✭ 75 (+275%)
Mutual labels:  events
ionic-image-upload
Ionic Plugin for Uploading Images to Amazon S3
Stars: ✭ 26 (+30%)
Mutual labels:  javascript-library
react-compose-events
A Higher-Order Component factory to attach outside event listeners
Stars: ✭ 25 (+25%)
Mutual labels:  events
sqlweb
SqlWeb is an extension of JsStore which allows to use sql query for performing database operation in IndexedDB.
Stars: ✭ 38 (+90%)
Mutual labels:  javascript-library
NYC Taxi Pipeline
Design/Implement stream/batch architecture on NYC taxi data | #DE
Stars: ✭ 16 (-20%)
Mutual labels:  events
ethairballoons
A strictly typed ORM library for Ethereum blockchain.
Stars: ✭ 27 (+35%)
Mutual labels:  javascript-library
nojs
Library that helps minimize js you have to write
Stars: ✭ 118 (+490%)
Mutual labels:  javascript-library
react-picture-annotation
A simple annotation component.
Stars: ✭ 53 (+165%)
Mutual labels:  javascript-library
aws-amplify-react-custom-ui
Building a Custom UI Authentication For AWS Amplify
Stars: ✭ 21 (+5%)
Mutual labels:  javascript-library
safe-touch
⛓ Runtime optional chaining for JS
Stars: ✭ 71 (+255%)
Mutual labels:  javascript-library
agones-event-broadcaster
Broadcast Agones GameServers and Fleets states to the external world
Stars: ✭ 22 (+10%)
Mutual labels:  events
DreamForgerJS
Interactive Fiction JS library
Stars: ✭ 14 (-30%)
Mutual labels:  javascript-library
oojs-ui
OOUI is a modern JavaScript UI library with strong cross-browser support. It is the standard library for MediaWiki and Wikipedia. This is a mirror from https://gerrit.wikimedia.org. Main website:
Stars: ✭ 45 (+125%)
Mutual labels:  javascript-library
GifWriter.js
GIF (version 89a) Encoder written in TypeScript
Stars: ✭ 41 (+105%)
Mutual labels:  javascript-library
v-bucket
📦 Fast, Simple, and Lightweight State Manager for Vue 3.0 built with composition API, inspired by Vuex.
Stars: ✭ 42 (+110%)
Mutual labels:  javascript-library
event-worker
A simpler way of dealing with Web Workers
Stars: ✭ 18 (-10%)
Mutual labels:  events
events-manager-io
A basic site for managing event centers and scheduling events.
Stars: ✭ 19 (-5%)
Mutual labels:  events

rel-events

Build Status Coverage Status

To read the docs, go to our docs folder. :) To see the app demo, go to our codesandbox page!

Welcome to the rel-events github repo!

rel-events is an awesome events library for react. It uses redux behind the scenes to offer a more convenient, simple and relevant API for dealing with data flows.

Basic Usage

Installing

To install rel-events, just run npm i --save rel-events.

If you wish to use Events to make HTTP Requests (which you probably do), there's another step to set things up. Follow our HTTPEvents Guide to have everything ready. :)

With that done, you may start to create some events!

Creating a basic Event

Let's say you want to pass a range of dates from DatePickerComponent to CalendarComponent. Instead of creating actions and reducers, forget everything about redux; create an Event instead.

To do that, you need to initialize a new Event. It's recommended you create it in a new file (events.js).

// on events.js
import { Event } from 'rel-events';

export const ChooseDateRangeEvent = new Event({
  name: 'chooseDateRange',
  manager: {
    initialState: {},
    onDispatch: (state, event) => {
      return {
        ...state,
        startDate: event.startDate,
        endDate: event.endDate,
      }
    }
  }
});

Let's break step-by-step what this code means:

First, you import the Event class from our lib, then instantiate a new event. This Event receives an object with two required keys: name and manager. While name is self-explanatory, manager is not.

For default Events, an Event Manager should have an initialState and implement an onDispatch method, which will be called whenever the event is dispatched. This is the alternative to the reducer part of the default redux flow.

We recommend using classes for your EventManagers as well, since we can decouple Events from their managers.

// on eventManagers.js
export class ChooseDateRangeEventManager {
  initialState = {};

  onDispatch = (state, event) => {
      return {
        ...state,
        startDate: event.startDate,
        endDate: event.endDate,
      }
    }
  }
}

Then:

// on events.js
import { Event } from 'rel-events';
import { ChooseDateRangeEventManager } from './eventManagers.js';

export const ChooseDateRangeEvent = new Event({
  name: 'chooseDateRange',
  manager: new ChooseDateRangeEventManager(),
);

Hooking it up with redux

With the event instantiated, you need to hook it up to redux so it can be dispatched and save data. When creating your root reducer, you should import the Event and initialize its reducers.

// on myAppRootReducers.js
import { combineReducers } from 'redux';
import { combineEventReducers } from 'rel-events';
import { ChooseDateRangeEvent } from './events.js';

// remember to use object spread, so it's set up correctly
export default combineReducers({
  ...combineEventReducers([ ChooseDateRangeEvent ]),
});

Notice the store names and reducers aren't declared anymore; you don't need to. Any Event object will deal with anything and everything redux related. To be able to do that, you only need to hook it to redux as the example above. To see more on how this works, read our how it works docs.

Now you have our Event ready to go! Now, you just need to register it to a Component, which can trigger it and/or listen to it.

Registering components to Events

Let's say you have a component called DatePickerComponent that knows how to render a beautiful date picker. It has a handleDatesChange method to update the state with the new dates.

export default class DatePickerComponent extends React.Component {
  //...
  handleDatesChange = (startDate, endDate) => {
    this.setState({ startDate, endDate });
  }
  //...
}

To be able to send data from this component to the CalendarComponent, you may register both Components to your Event. Whenever you register a Component to an Event, you automatically receive a function to trigger the event as a prop. The function's name is the same as the event name you passed when initializing the event.

import { ChooseDateRangeEvent } from './events.js';

// you won't export the component directly anymore
class DatePickerComponent extends React.Component {
  //...
  handleDatesChange = (startDate, endDate) => {
    // here, the event passing the new dates is triggered
    // after setState is done
    this.setState(
      { startDate, endDate },
      () => this.props.chooseDateRange({ startDate, endDate })
    );
  }
  //...
}

// and here, you register the component to the event.
// since Components are mostly named with CamelCase,
// we preferred to name the key like that as well
export default ChooseDateRangeEvent.register({
  Component: DatePickerComponent,
});

// you may as well register a Component to multiple events, no worries;
// just remember to only export after you're done registering the Component to your events

Then, you may register your CalendarComponent as well, but passing a new props key:

import { ChooseDateRangeEvent } from './events.js';

class CalendarComponent extends React.Component {
  //...
  render(){
    const { startDate, endDate } = this.props;

    return <h1>The dates are: {startDate}, {endDate}</h1>
  }
}

// and here, you get the props from the event
export default ChooseDateRangeEvent.register({
  Component: CalendarComponent,
  props: ['startDate', 'endDate'],
})

And that's it! We still have a lot of other features to discuss, but I'll talk about those later. Before that, let's talk about using events to make HTTP requests.

Contributing

Thanks for wanting to contribute! Please, read our Contributing guide carefully before opening PRs, though. We do enjoy PRs, but any PRs that don't follow our contributing guidelines will probably be rejected :/

Thanks

Thanks for everyone at Labcodes for giving me the structure and time for me to work on this pet project of mine and giving me lots of awesome feedback! Bernardo Fontes, Luan Fonseca, Thulio Philipe, Mariana Bedran, Breno Chamie and Renato Oliveira, I'm really, really grateful for your input! <3

labcodes github banner

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].