1.2.0 • Published 2 years ago

@dogonis/react-injectable v1.2.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

Injectables (DI/IoC) for React


Includes:
  • Injectable - abstract class for injectable creation
  • useInjector - getter hook for an Injectable
  • InjectableProvider - IoC container/provider
  • OnInit - lifecicle onInit hook

Injectable creation
export class TestService extends Injectable<{ counter: number }> {
    getDefaultState(){
        return {
            counter: 0
        }
    }

    increment(){
        this.setState(prev => ({
            counter: prev.counter + 1
        }))
    }
}
Injectable usage
// initialization

render(
    <InjectableProvider inject={[TestService, SecondService]}>
        <App />
    </InjectableProvider>
)

// react flow usage
const App = () => {

    const [state, counter] = useInjector(TestService)

    return (
        <div onClick={() => counter.increment()}>
            {state.counter}
        </div>
    )
}

// usage in an injectable
class SecondService extends Injectable implements OnInit{
    getDefaultState(){}

    onInit(){
        this.getInjection(TestService)
            .asObservable()
            .pipe(
                take(12)
            )
            .subscribe(console.log)
    }

}
1.2.0

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

3 years ago

1.0.0

3 years ago

0.0.1

3 years ago