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

reactive-props

v0.2.2

Published

An all-in-one implementation of the Reactive State for Data & DOM patterns.

Downloads

1,623

Readme

reactive-props

Build Status Coverage Status

An all-in-one implementation of the Reactive State for Data & DOM patterns, compatible down to IE9.

Live Examples

API

This module exports a default helper function that can create utilities to define reactive properties / states for data or DOM elements. For documentation sake, this function will be named createHandler, and it accepts an optional configuration object, with the following properties:

  • all:boolean, signals that all set properties should invoke the related update. If true, even if a property has the same value it had before, the related update function will be invoked.
  • shallow:boolean, signals that that even if the property value is the same, the update should happen in case it's the same object, or the same array, set before. If false, and all = false too (default), no update happens in case the object is the exact same as before.
  • dom:boolean, signals that properties will be attached to a DOM element, which needs to be passed along. By default, the returned utility to create reactive properties has a (props[, update]) signature, but when dom = true, the returned helper will have a (element, props[, update]) signature. By default, dom is `false.
  • getAttribute(element, key):any is an optional helper to retrieve the right value when dom = true and the element already had an attribute with the reactive property name. <element checked="true"> will pass to this helper the element reference and the checked attribute name. By default, this helper returns element.getAttribute("checked"), but it is possible to return JSON.parse(element.getAttribute("checked")) instead, so that the initial element.checked will return a proper boolean value.
  • useState(value):void is an optional helper that accepts any generic useState handler from any hooks based library. If provided, it will be invoked passing along the new value when all conditions are match (see previous all and shallow description)

The resulting helper returns either the state object with reactive properties, or the passed element.

// for reactive states
const reactiveProps = createHandler();
const state = reactiveProps({...}, update);

// for reactive elements
const reactiveElement = createHandler({dom: true});
const el = reactiveElement(document.querySelector('el'), {...}, update);

If an update function is provided, it will be used to invoke state changes per each update, bypassing the possible useState.

import {useState} from 'augmentor';

const reactiveProps = createHandler({useState});
const state = reactiveProps({test: ''});
state.test = 'OK';
// will invoke useState('OK')

const overload = reactiveProps({test: ''}, console.log);
overload.test = 'OK';
// will simply log "OK" without invoking useState("OK")

Default Use Cases

The default value goal of all options is to cover these common use cases:

  • primitive properties that would trigger updates only if different
  • non immutable data that would trigger updates if properties are objects/arrays. Use shallow = false option if data is granted to be immutable deep down each inner value
  • integrated hooks to work within a variety of libraries that offer a useState hook

For any other combined use case, please refer to the related post and find out your fine tuned reactive state handler.

Partial Imports

If all you need is either the state handler or the dom handler, it is possible to import just those two separately, resulting in a slightly smaller bundle.

// const genericHandler = require('reactive-props');
import genericHandler from 'reactive-props';

// const domHandler = require('reactive-props/dom');
import domHandler from 'reactive-props/dom';

// produces same results
domHandler();
genericHandler({dom: true});

// const stateHandler = require('reactive-props/state');
import stateHandler from 'reactive-props/state';

// produces same results
stateHandler();
genericHandler({dom: false});

Basic Example

// const createHandler = require('reactive-props');
import createHandler from 'reactive-props';

const reactiveProps = createHandler();
const reactiveElement = createHandler({dom: true});

// create reactive props
const state = reactiveProps(
  // props to react for
  {test: ''},
  // called on each prop update
  () => console.log(state)
);

state.test;           // ""
state.test = 'value'; // {"test":"value"}

// create reactive elements
const body = reactiveElement(
  document.body,
  {test: ''},
  () => console.log('body.test', body.test)
);

body.test;           // ""
body.test = 'value'; // body.test "value"