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

ireactivity

v1.0.3

Published

iReactivity - Simple/lightweight (~3kb) React binding

Downloads

32

Readme

iReactivity

Simple / lightweight (~3kb) React binding.

Counter example #1 - classes:

import React from 'react';
import ReactDOM from 'react-dom';
import {Provider, connect} from 'ireactivity/es5';

// Logic
class Counter {
  constructor() {
    this.counter = 0;
  }

  onUp() {
    this.counter++;
  }
}

// Store
const store = {
  counter1: new Counter()
};

// View
const CounterView = ({counter, onUp}) =>
  <div>
    <h1>Counter App</h1>
    <p>{counter}</p>
    <button onClick={onUp}>up</button>
  </div>;

// Connection #1
const AppCounter1 = connect(CounterView,
  (store) => store.counter1);

// Connection #2
const AppCounter2 = connect(CounterView,
  (store) => new Counter());


ReactDOM.render(
  <Provider store={store}>
    <div>
      <AppCounter1/>
      <AppCounter2/>
    </div>
  </Provider>,
  document.getElementById('root')
);

Counter example #2 - objects:

// index.js

import React from 'react';
import ReactDOM from 'react-dom';
import {Provider, connect} from 'ireactivity';

import AppView from './App';

const store = {
    counter: 0
};

const App = connect(AppView, {
    counter: (store) => store.counter,
    onUp: (store) => () => store.counter = store.counter + 1
});

ReactDOM.render(
    <Provider store={store}><App /></Provider>,
    document.getElementById('root')
);

// App.js

import React  from 'react';

export default ({counter, onUp}) =>
    <div className="counter-box">
        <div className="counter">{counter}</div>
        <button className="button" onClick={onUp}>UP</button>
    </div>

Documentation

In simple words

The main idea of this way:

  • Every object can be Store. Store is state of your app or state of your component.
  • You can connect any component to this Store.
Store

Every object is state. Store is state of app.

const store = { user: { name: 'slava'}, project: { name: 'iReactivity'} };
Provider

Provider provides store to the app and for the all components of app.

// ....
ReactDOM.render(
    <Provider store={store}><App/></Provider>, document.getElementById('root'));
Connect

You can connect part of your state to the component. Like this.

const ProjectView = ({name, onOk}) =>
    <div> {name} <button onClick={onOk}>OK</button> </div>;

const Project = connect(ProjectView, {
    name: (store) => store.project.name,
    onOk: (store) => () => store.project.name = 'iReactivity OK'
});

When you click on OK it updates store and UI react on this (store.project.name = 'iReactivity OK'). For each action from user side it calls update method.

Connect to object

Same example but with isolated class

class ProjectLogic {
  constructor(){
    this.name = 'iReactivity'
  }
  
  onOk() {
    this.name = 'iReactivity OK'        
  }
}

const ProjectView = ({name, onOk}) =>
    <div> {name} <button onClick={onOk}>OK</button> </div>;

const Project = connect(ProjectView, (store) => new ProjectLogic());
Update

It's the event that notifies store. When you call update(store), connected component will try to react if there is some changes. This example updates store without action from user side.

setTimeout(() => {
    // update(store, updaterFunction)
    update(store, (store) => {
        store.project.name = 'iReactivity UPDATED';
    });
}, 30 * 1000);

The UI will react after 30s. This method is helpful when you work with socket.io for example or some libs that is not connected to React at all.

Update + Connect + Promises

For example when you need to make API call or make something async:

Just return promise and it will wait for your updates.

update(store, (store) => {
    store.project.name = 'iReactivity Loading.. . .';
    return doSometingAsync().then(() => store.project.name = 'iReactivity UPDATED')
});

Connect works in the same way because uses update method on actions.

const Project = connect(ProjectView, {
    name: (store) => store.project.name,
    onOk: (store) => () => doSometingAsync().then(() => store.project.name = 'iReactivity OK')
});

Hello world example:

import React from 'react';
import ReactDOM from 'react-dom';

import {Provider, connect, update} from 'ireactivity';

const store = { name: 'Hello' };

const AppView = ({name, onHello}) =>
    <button onClick={onHello}> {name} </button>;

const App = connect(AppView, {
    name: (store) => store.name,
    onHello: (store) => () => store.name += ' World!'
});

ReactDOM.render(
    <Provider store={store}><App/></Provider>, document.getElementById('root'));

// if you need to update store outside of components
setTimeout(() => {
    update(store, (store) => {
        store.name = 'Something NEW!!!';
    });
}, 30 * 1000);

Examples

Thanks +1G