# Hooks

For the concept and usage of React Hooks, please refer to official documents (opens new window).

The following APIs are included in the @vesselize/react (opens new window) NPM package.

# useInstance(token, [context])

  • Parameters

    • {string | symbol | Class} token
    • {Object} context
      • {number} id
  • Returns

Return the provider instance found by token.

  • Usage

It can be used in the component's setup method to obtain an instance.

const userService = useInstance('UserService');

// in specific context
const currentUserContext = {
  id: 123456
};
const currentUserService = useInstance('UserService', currentUserContext);

# useAsyncInstance(token, [context])

  • Parameters

    • {string | symbol | Class} token
    • {Object} context
      • {number} id
  • Returns

Return the promise that will resolve the provider instance.

  • Usage
const userServicePromise = useAsyncInstance('UserService');

useEffect(() => {
  userServicePromise.then(userService => {
    // do something with userService
  });
}, []);

# useProvider(token)

  • Parameters

    • {string | symbol | Class} token
  • Returns

Return the provider class or factory.

  • Usage

Create a custom instance by yourself through a class or factory.

const UserService = useProvider('UserService');

const customUserService = new UserService({ name: 'Felix Yang' });

# useVesselize

  • Returns

Vesselize instance.

  • Usage

Get the instance of vesselize and do whatever you want.

const vesselize = useVesselize('UserService');

const userService = vesselize.get('UserService');