Security News
npm Updates Search Experience with New Objective Sorting Options
npm has a revamped search experience with new, more transparent sorting options—Relevance, Downloads, Dependents, and Publish Date.
stated-bean
Advanced tools
[![Travis](https://img.shields.io/travis/com/mjolnirjs/stated-bean.svg)](https://travis-ci.com/mjolnirjs/stated-bean) [![Codecov](https://img.shields.io/codecov/c/gh/mjolnirjs/stated-bean)](https://codecov.io/gh/mjolnirjs/stated-bean) [![type-coverage](ht
A light but scalable state management library with react hooks, inspired by unstated-next. that allows you to manage the state data of multiple views together. Make cross-component data transfer simple.
# yarn
yarn add stated-bean
# npm
npm i stated-bean
GitHub Pages: Integration with injection-js
import { StatedBean, Stated, useBean } from 'stated-bean';
@StatedBean()
export class Counter {
@Stated()
count: number = 0;
increment() {
this.count++;
}
decrement() {
this.count--;
}
}
function CounterDisplay() {
const counter = useBean(Counter);
return (
<div>
<button onClick={counter.decrement}>-</button>
<span>{counter.count}</span>
<button onClick={counter.increment}>+</button>
</div>
);
}
StatedBean
Signature: @StatedBean(name?: string | symbol): ClassDecorator
Indicates that an annotated class is a StatedBean
. The name
may indicate a suggestion for the bean name. Its default value is Class.name
.
Stated
Signature: @Stated(): PropertyDecorator
Indicates that an annotated property is Stated
. Its reassignment will be observed and notified to the container.
PostProvided
Signature: @PostProvided(): MethodDecorator
The PostProvided
decorator is used on a method that needs to be executed after the StatedBean
be instanced to perform any initialization.
Effect
Signature: @Effect(name?: string | symbol): MethodDecorator
The Effect
decorator is used on a method that can get the execution state by useObserveEffect
.
useBean
Signature: useBean<T>(typeOrSupplier: ClassType<T> | () => T, name?: string | symbol): T
The useBean
will create an instance of the stated bean with a new StatedBeanContainer
and listen for its data changes to trigger the re-rendering of the current component.
useInject
Signature: useInject<T>(type: ClassType<T>, option: UseStatedBeanOption<T> = {}): T
The useInject
will get the instance of the stated bean from the StatedBeanContainer
in the context and listen for its data changes to trigger the re-rendering of the current component.
React Context
function SampleComponent() {
const model = useInject(UserModel);
// ...
}
function App() {
return (
<StatedBeanProvider types={[UserModel]}>
<SampleComponent />
</StatedBeanProvider>
);
}
Component
function SampleComponent() {
const model = useBean(() => new UserModel());
// pass the model to its children
return <ChildComponent model={model} />;
}
UseStatedBeanOption
option = {
name: string | symbol; // get/create the instance with special name
dependentFields: Array<string | symbol>; // do re-render when the special property changed
};
useObserveEffect
Signature: useObserveEffect(bean: StatedBeanType, name: string | symbol): EffectAction
observe the execution state of the method which with @Effect
.
@StatedBean
class UserModel {
@Effect()
fetchUser() {
// ...
}
}
const UserInfo = () => {
const model = useBean(() => new UserModel());
const { loading, error } = useObserveEffect(model, 'fetchUser');
// ...
};
<StatedBeanProvider {...props: StatedBeanProviderProps} />
The StatedBeanProvider
is responsible for creating an instance of the stated bean and dispatching an event after data changes.
StatedBeanProviderProps
interface StatedBeanProviderProps {
types?: ClassType[];
beans?: Array<StatedBeanType<unknown>>;
beanProvider?: BeanProvider;
application?: StatedBeanApplication;
}
0.3.0-beta (2019-09-08)
FAQs
A light but scalable state management library with react hooks
The npm package stated-bean receives a total of 1 weekly downloads. As such, stated-bean popularity was classified as not popular.
We found that stated-bean demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers 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
npm has a revamped search experience with new, more transparent sorting options—Relevance, Downloads, Dependents, and Publish Date.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.