Skip to content

A JS library offering a hook for leveraging IoC capabilities in React frameworks.

License

Notifications You must be signed in to change notification settings

vgerbot-libraries/use-service

Repository files navigation

@vgerbot/use-service-react

Example

import { Observable, IoC, useService } from '@vgerbot/use-service-react';
import { observable, action } from 'mobx';

@Observable()
class UserService {
    @observable authorized = false;

    @action login() {
        this.authorized = true;
    }
    @action logout() {
        this.authorized = false;
    }
}

function App() {
    return <IoC>
        <UserStatus></UserStatus>
    </IoC>
}

const UserStatus = observer(() => {
    const userService = useService(UserService);
    return <div>{userService.authorized ? <Logout></Logout> : <Login></Login>}</div>
})

function Login() {
    const userService = useService(UserService);
    return <button onClick={() => { userService.login() }}>Login</button>
}
function Logout() {
    const userService = useService(UserService);
    return <button onClick={() => { userService.logout() }}>Logout</button>
}

About

A JS library offering a hook for leveraging IoC capabilities in React frameworks.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published