
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
usestate gives the webcomponents a new life cycle.
It is effective when doing data binding.
$ npm i -S usestate
import useState from 'usestate';
class Component extends HTMLElement {
/* ... */
}
export default useState(Component)
or
import useState from 'usestate';
@useState
export default class Component extends HTMLElement {
/* ... */
}
A component with usestate can use the following methods.
getState()setState(object)stateChangedCallback(stateName, oldValue, newValue)static get observedStatestateChangedCallback() 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().
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);
ISC
FAQs
usestate gives the webcomponents a new life cycle.
The npm package usestate receives a total of 109 weekly downloads. As such, usestate popularity was classified as not popular.
We found that usestate demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.