npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bly-react-mixin

v1.1.0

Published

React mixin to easily integrate with the Bly flux framework

Downloads

4

Readme

React mixin to easily integrate with the Bly flux framework Build Status

Bly and React make for a really good couple, it's what Bly was designed with in mind. This mixin is the glue to their relationship, letting you access stores and dispatch actions from your React components.

Usage

Simply pass in your Bly app to your top level component using the 'bly' prop. This will give it and all of it's children that also use the mixin access to Bly's app.stores (read-only) and app.inject. Do this in Bly's app.render and you're all golden.

var Bly = require('bly');
var React = require('react');
var BlyReactMixin = require('bly-react-mixin');
var MessagesStore = require('./stores/messages-store');

var YourAppComponent = React.createClass({

	mixins: [BlyReactMixin],
	
	getInitialState: function() {
		// access a store of your bly app
		return {
			latestMessage: this.stores('messages').last()
		};
	},

	render: function() {

		return React.DOM.div({
			onClick: this.onClickMessage
		}, this.state.lastMessage.text);

		// could have been JSX just as easily
		// <div onClick={this.onClickMessage}>
		//	{this.state.lastMessage.text}
		// </div>
	},

	onClickMessage: function(e) {
		// dispatch actions
		this.inject('MESSAGE_READ', this.state.latestMessage.id);
	}
});


var app = new Bly.App();

app.stores('messages', new MessagesStore());
app.action({
	name: 'MESSAGE_READ',
	handler: function(waitFor, messageId) {
		app.stores('messages').onRead(messageId);
	}
});

// setup rendering 

app.render(function() {
	
	React.renderComponent(YourAppComponent({
		bly: app
	}), document.documentElement);
});

Components created outside of components' render method

As long as the components are created inside the top level component or one of it's children, you don't have to pass around the bly prop manually. However, if a component is created somewhere else, you'll have to pass the app using the bly prop manually. The easiest way is to use the provided cloneWithBly method.

var React = require('react');
var BlyReactMixin = require('bly-react-mixin');


var MessageComponent = React.createClass({
	mixins: [BlyReactMixin],

	getInitialState: function() {
		return {
			latestMessage: this.stores('messages').last()
		};
	};

	render: function() {
		return React.DOM.div({}, 'foo');
	}
});

// When creating the child inside a component, you don't have to pass it manually
var FirstMessageApp = React.createClass({
	mixins: [BlyReactMixin],

	render: function() {
		// no need to manually pass bly
		return React.DOM.div({},
			MessageComponent()
		);
	}
});


// If the component is created elsewhere, outside of a component with access to the Bly app, 
// your bly instance needs to be passed in. You can do this manually, but can also use the 
// provided `this.cloneWithBly` for convenience

var SecondMessageApp = React.createClass({
	mixins: [BlyReactMixin],

	render: function() {
		// 
		return React.DOM.div({},
			this.cloneWithBly(this.props.children) // injects bly into the component(s) we're rendering
		);
	}
});

API

var store = component.stores(name)

Retrieve a reference to a store instance.

  • name - (required) name of the store you want to retrieve.

component.inject(action, payload)

Inject an action into the system to be dispatched. The dispatching of an action is synchronous and only one action can be dispatched at a time.

  • action - (required) the name of the action you want to inject. For example RECEIVE_MESSAGES.
  • payload - the payload of the action, can be any value. Defaults to an empty object {}.

component.inject(actionCreator [, args...])

Let an 'action creator' (read: function) handle the dispatching of an action, which is especially useful with the more complex actions. All other arguments will be passed to the action creator.

  • actionCreator - (required) a function with the signature function(app, args..) where:
    • app - instance of your bly app
    • args... - any arguments passed to component.inject after the action creator

var unsubscribe = component.subscribe(renderCallback)

Subscribe directly to the app.render callback of the Bly app by passing a function. Useful if you really need components deeper down in the tree to update themselves instead of receiving updates from the top down.

  • renderFunction - (required) function with signature function(results) where:
    • results - object with results generated by functions registered with app.results

Returns an unbscribe function which when called will stop the callback from being called ever again. Automatically unsubscribes when component will unmount.