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

usestate

v1.1.3

Published

usestate gives the webcomponents a new life cycle.

Downloads

1,041

Readme

usestate

usestate gives the webcomponents a new life cycle.

It is effective when doing data binding.

$ npm i -S usestate

Usage

import useState from 'usestate';

class Component extends HTMLElement {
	/* ... */
}

export default useState(Component)

or

import useState from 'usestate';

@useState
export default class Component extends HTMLElement {
	/* ... */
}

What can I do with it?

A component with usestate can use the following methods.

  • getState()
  • setState(object)
  • stateChangedCallback(stateName, oldValue, newValue)
  • static get observedState

stateChangedCallback() is called when a state is changed. Only called for observed state. So stateChangedCallback() is like attributeChangedCallback().

You can use setState() to change a state. setState() find the difference of state.

You should define the state to be observed by observedState() method.

observedState() method is a static getter method that returns an array of state names like observedAttributes().

example

import useState from 'usestate'

class HelloElement extends HTMLElement {
	constructor() {
		super();
		
		const input = document.createElement('input');
		input.type = 'text';
		input.onkeyup = (e) => {
			this.setState({ name: e.target.value });
		}
		this.appendChild(input);
		
		this.p = document.createElement('p');
		this.appendChild(this.p);
	}

	stateChangedCallback(stateName, oldValue, newValue) {
		switch(stateName) {
			case 'name':
				this.p.textContent = `Hello, ${newValue}`;
		}
	}

	static get observedState() { return ['name']; }
}

customElements.define('hello-element', HelloElement);

const helloElement = new (useState(HelloElement));
document.body.appendChild(helloElement);

helloElement.setState({ name: 'world' });

With flux.

import EventEmitter from 'events'
import useState from 'usestate'

const dispatcher = new EventEmitter();

class Store extends EventEmitter {
	constructor() {
		super();

		this.name = '';

		dispatcher.on('inputName', (value) => {
			this.name = value;
			this.emit('CHANGE');
		});
	}
}
const store = new Store()

class HelloElement extends HTMLElement {
	constructor() {
		super();

		this.handleStoreChange = this.handleStoreChange.bind(this);
		
		const input = document.createElement('input');
		input.type = 'text';
		input.onkeyup = (e) => {
			dispatcher.emit('inputName', input.value);
		}
		this.appendChild(input);
		
		this.p = document.createElement('p');
		this.appendChild(this.p);
	}

	handleStoreChange() {
		this.setState(store);
	}

	connectedCallback() {
		store.on('CHANGE', this.handleStoreChange);
		//initialization
		this.handleStoreChange();
	}

	disconnectedCallback() {
		store.removeListener('CHANGE', this.handleStoreChange);
	}

	stateChangedCallback(stateName, oldValue, newValue) {
		switch(stateName) {
			case 'name':
				this.p.textContent = `Hello, ${newValue}`;
		}
	}

	static get observedState() { return ['name']; }
}

customElements.define('hello-element', HelloElement);

const helloElement = new (useState(HelloElement));
document.body.appendChild(helloElement);

License

ISC