All Projects → fahad19 → Proppy

fahad19 / Proppy

Licence: mit
Functional props composition for UI components (React.js & Vue.js)

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Proppy

Marble
Marble.js - functional reactive Node.js framework for building server-side applications, based on TypeScript and RxJS.
Stars: ✭ 1,947 (+111.4%)
Mutual labels:  rxjs, functional-programming
Corsair
Corsair using RxJS, Immutable.js and WebGL/three.js
Stars: ✭ 36 (-96.09%)
Mutual labels:  rxjs, functional-programming
Refract
Harness the power of reactive programming to supercharge your components
Stars: ✭ 791 (-14.12%)
Mutual labels:  rxjs, preact
Preact Redux Isomorphic
preact-redux-isomorphic PWA SPA SSR best practices and libraries in under 80kB page size (for live demo click the link below)
Stars: ✭ 85 (-90.77%)
Mutual labels:  rxjs, preact
Cyclejs.cn
The Cycle.js Chinese documentation website.
Stars: ✭ 132 (-85.67%)
Mutual labels:  rxjs, functional-programming
Cyclejs
A functional and reactive JavaScript framework for predictable code
Stars: ✭ 9,996 (+985.34%)
Mutual labels:  rxjs, functional-programming
Inferno Most Fp Demo
A demo for the ReactJS Tampa Bay meetup showing how to build a React+Redux-like architecture from scratch using Inferno, Most.js, reactive programmning, and various functional programming tools & techniques
Stars: ✭ 45 (-95.11%)
Mutual labels:  rxjs, functional-programming
Redux Most
Most.js based middleware for Redux. Handle async actions with monadic streams & reactive programming.
Stars: ✭ 137 (-85.12%)
Mutual labels:  rxjs, functional-programming
bassdrum
reactive, type safe components with preact and rxjs.
Stars: ✭ 44 (-95.22%)
Mutual labels:  rxjs, preact
Learn Rxjs Operators
Learn RxJS 中文版 (通过清晰的示例来学习 RxJS 5 操作符)
Stars: ✭ 789 (-14.33%)
Mutual labels:  rxjs
Push State
Turn static web sites into dynamic web apps.
Stars: ✭ 16 (-98.26%)
Mutual labels:  rxjs
Rxjs Docs Cn
RxJS 5 中文文档
Stars: ✭ 786 (-14.66%)
Mutual labels:  rxjs
Kaur
A bunch of helper functions to ease the development of your applications.
Stars: ✭ 17 (-98.15%)
Mutual labels:  functional-programming
Ngx Restangular
Restangular for Angular 2 and higher versions
Stars: ✭ 787 (-14.55%)
Mutual labels:  rxjs
Txmonad
A toy xmonad
Stars: ✭ 22 (-97.61%)
Mutual labels:  functional-programming
Pointfreeco
🎬 The source for www.pointfree.co, a video series on functional programming and the Swift programming language.
Stars: ✭ 782 (-15.09%)
Mutual labels:  functional-programming
Fp Core.rs
A library for functional programming in Rust
Stars: ✭ 772 (-16.18%)
Mutual labels:  functional-programming
Tanok
Elm Architecture-inspired wrapper for Rx.js+React
Stars: ✭ 22 (-97.61%)
Mutual labels:  rxjs
Es Cqrs Shopping Cart
A resilient and scalable shopping cart system designed using Event Sourcing (ES) and Command Query Responsibility Segregation (CQRS)
Stars: ✭ 19 (-97.94%)
Mutual labels:  functional-programming
Gifcurry
😎 The open-source, Haskell-built video editor for GIF makers.
Stars: ✭ 830 (-9.88%)
Mutual labels:  functional-programming

ProppyJS

Functional props composition for components
A 1.5kB library integrating with your favourite UI framework

Built with ❤︎ by @fahad19 and contributors

What is ProppyJS?

ProppyJS is a tiny 1.5kB JavaScript library for composing props (object that components receive to render themselves).

It comes with various integration packages giving you the freedom to use it popular rendering libraries.

What does it do?

ProppyJS flow of props

The idea is you express the behaviour of your Component as props first, and then connect it to your Component (which can be either React, Vue.js or Preact) using the same API of Proppy.

The API gives you access to other application-wide dependencies too (like a store using Redux) for convenience anywhere in the components tree.

Packages

Package Status Size Description
proppy proppy-status 1.5K Core package
proppy-react proppy-react-status 1.0K React integration
proppy-vue proppy-vue-status 0.7K Vue.js integration
proppy-preact proppy-preact-status 1.1K Preact integration
proppy-redux proppy-redux-status 0.6K Redux integration
proppy-rx proppy-rx-status 0.6K RxJS integration

Quick start

Installation

With npm:

$ npm install --save proppy

Basics

Factories

For the simplicity of this example, we can start with withProps function from proppy first:

import { withProps } from 'proppy';

const P = withProps({ counter: 1 });

Instances

Now we can get an instance from our factory function by calling it:

const p = P();

To access the props synchronously:

console.log(p.props); // { counter: 1 }

Subscription

Given the nature of our instance having static props, if we subscribe to it, it will emit the props only once:

const unsubscribe = p.subscribe(props => console.log(props));
// { counter: 1 }

To unsubscribe, just call the returned function:

unsubscribe();

Destroy

To cancel all listeners and clear the internal state, just call:

p.destroy();

Dynamic props

There are times when your props require a bit of interactivity. Imagine your component wants to render the current state of counter, but also want to be able to update the value.

We can achieve that using the withState function for example:

import { withState } from 'proppy';

// withState(stateName, setterName, initialState)
const P = withState('counter', 'setCounter', 0);

const p = P();

Initially, the instance will only show you the default initial state for counter:

console.log(p.props);
// { counter: 0, setCounter: Function }

But we can update it too:

p.props.setCounter(5);

console.log(p.props);
// { counter: 5, setCounter: Function }

If you subscribed to the instance, it will keep emitting the new props every time a change occurs:

p.subscribe(props => console.log(props));
// { counter: 0, setCounter: Function }
// { counter: 5, setCounter: Function }

Composition

Proppy also ships with a handy compose function which allows you to compose multiple factories into a single one:

import { compose, withProps, withState } from 'proppy';

const P = compose(
  withProps({ foo: 'foo value' }),
  withProps({ bar: 'bar value' }),
  withState('counter', 'setCounter', 0)
);

Once you create an instance, all the props from those factories will be accessible in a single object:

const p = P();

console.log(p.props);
// {
//   foo: 'foo value',
//   bar: 'bar value',
//   counter: 0,
//   setCounter: Function
// }

Providers

Providers are application-wide dependencies that all Proppy instances can access anywhere.

They are useful especially when you want to maintain them in a single place, but you want Components from anywhere to access them at will.

Imagine setting your application's name, in a providers object like this:

const myProviders = {
  appName: 'My Super Awesome App!'
};

Now when composing your props with Proppy, you want to be able to access them:

import { compose, withProps } from 'proppy';

const P = compose(
  withProps({ foo: 'foo value' }),

  // `withProps` can also generate props using a function
  withProps((props, providers) => {
    return {
      name: providers.appname
    };
  })
);

// pass `myProviders` when calling the factory
const p = P(myProviders);

Your Proppy instance now has these props:

console.log(p.props);
// {
//   foo: 'foo value',
//   name: 'My Super Awesome App!'
// }

Rendering with React

Proppy integrates with React via proppy-react package.

You can install it with npm first:

$ npm install --save react proppy-react

Set providers

Then we need to set our custom providers in our root React component:

// components/Root.js
import React from 'react';
import { ProppyProvider } from 'proppy-react';

import Counter from './Counter';

const myProviders = {};

export default function Root() {
  return (
    <ProppyProvider providers={myProviders}>
      <Counter />
    </ProppyProvider>
  );
}

Attach factory

Now from anywhere in our components tree, we can use the attach higher-order component to connect your Proppy factory to React component:

// components/Counter.js
import React from 'react';
import { attach } from 'proppy-react';
import { compose, withProps, withState } from 'proppy';

const P = compose(
  withProps({ foo: 'foo value' }),
  withState('counter', 'setCounter', 0),
);

function Counter(props) {
  const { foo, counter, setCounter } = props;

  return (
    <div>
      <p>Foo: {foo}</p>

      <p>Counter: {counter}</p>

      <button onClick={() => setCounter(counter + 1)}>
        Increment
      </button>
    </div>
  );
}

export default attach(P)(Counter);

You can do the same with Vue.js and Preact too via proppy-vue and proppy-preact packages respectively.

Inspiration

Original inspiration for this project has been recompose. If your project is using React, you should definitely check it out.

Learn more about the differences between Proppy and other libraries (like Redux and Recompose) in our FAQ page.

License

MIT

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