Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jonabrams/friendlyfire
Simple pubsub/event system for React component communication
https://github.com/jonabrams/friendlyfire
Last synced: about 1 month ago
JSON representation
Simple pubsub/event system for React component communication
- Host: GitHub
- URL: https://github.com/jonabrams/friendlyfire
- Owner: JonAbrams
- Created: 2017-02-25T23:56:35.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-02-27T06:14:20.000Z (almost 8 years ago)
- Last Synced: 2024-05-09T18:12:45.339Z (8 months ago)
- Language: JavaScript
- Homepage:
- Size: 5.86 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# FriendlyFire
Easy cross-component communication using pubsub, designed for React. No dependencies.
## Why?
React provides a built-in mechanism for parent components to communicate with child components: props.
In order to allow components to communicate with parent, sibling, or unrelated components, the are various solutions;
most famously Flux. While it's a great solution, especially for large and complex projects, it requires a lot
of boilerplate code, and spreads logic across many files. Perhaps most annoying of all, it requires parent
components to send callback handlers to children, sometimes over many generations.Using FriendlyFire, components easily subscribe to events triggered by other components. Components
that trigger events don't need to know what will consume them, they don't need to add extra props for callbacks.Just by looking at a component's subscription methods, you can easily tell which events, emitted by
a particular component, it subscribes to. The emitting components on the other hand, need not care who subscribes.This model of communication enables modularity/portability of components, reducing the dependencies of components
on each other, making component dependency exist in only one direction, from parent to child.## Example
```javascript
import ff from 'friendlyfire';class ModalShower extends React.Component {
constructor() {
super();
this.state = { modalOpen: false };
}componentDidMount() {
ff.init(this); // Register with FriendlyFire to have subscribers registered
}// Auto-subscribes to 'close' event from Modal components
onModalClose() {
this.setState({ modalOpen: false });
}render() {
return(
Open Modal
Contents of modal
);
}
}class Modal extends React.Component {
componentDidMount() {
ff.init(this); // Register with FriendlyFire to make trigger(…) method available
}handleOuterClick(e) {
if (e.target === e.currentTarget) {
this.trigger('close');
}
}render() {
return (
{this.props.children}
)
}
}
```## API
### ff.init(_component_)
Initializes a react component to be able to publish and subscribe using FriendlyFire.
Recommended that you put this into `componentDidMount` and pass in `this` as the parameter.
Add `trigger` and will scan the components' methods looking potential subscribers, and use them.
### <component>.trigger(_eventName_)
Will fire any subscribers listening for the specified event from the triggering component.
### <component>.onComponentNameEventname(data)
To register a subscriber on a component, create a method on it using the above pattern. The above example will fire when another component in the same app called `ComponentName` fires the event `eventname`.
Note: The event specified needs to be all lowercase (with the exception of the first character).